Monday, December 7, 2015

File handling

File Handling :

File names could be specified in Unix format.
In order to work with a file, first one has to open a channel to the file...this is also called handle
Once the channel is opened, TCL retruns a channelId which is used to work with the files
Typical steps to be followed while working with files:
Step 1: Check whether the file specified exists or not
Step 2: Open a channel pointing to the file...TCL returns an ID
Step 3: Use the ID to work with the file
Step 4: Close the channel after use

File Open :

"open" could be used to open a channel to a file. This command returns the file-handle (channelID) which could be used to work with the files.
Syntax:
open fileName ?access mode?
where
fileName - is the name of the file to be opened for use
access mode - either r, r+, w, w+, a or a+. These are the access modes (similar to C language)
"open" opens a channel to the file specified by the fileName with the access permissions specified by the access mode. Default is "r".
A channel once opened, remains open until specifically closed.

File Close :

"close" is used to close a channel previously opened using the "open" command.
Syntax:
close fileID
where
fileID - is the channel ID of the file opened


Example :




some of the useful file operations are: (no need to open a channel for these operations)
file exists      --> checks if the specifie file exists or not
file dirname     --> returns the parent directory of the specified file
file extension   --> returns the extension (suffix) of the specified file (incl. dot)
file isfile      --> returns 1 if “name” is a normal file
file isdirectory --> returns 1 if “name” is a directory
file executable  --> returns 1 if “name” has executable permissions
file readable    --> returns 1 if “name” has read permissions
file writable    --> returns 1 if “name” has write permissions
file tail        --> returns the last pathname component of “name”

file rootname    --> returns all but the extension of the “name”

No comments: