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 " 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:
Example :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set ofile [open data.txt w] | |
for {set i 0 } { $i < 10 } { incr i } { | |
puts $ofile "This is line number : $i " | |
} | |
close $ofile | |
set ifile [open data.txt r] | |
while { [gets $ifile line] != -1 } { | |
puts $line | |
} | |
close $ifile |
No comments:
Post a Comment