top of page

File Handling

 

open

Syntax:
open fileName ?access? ?permissions?


fileName is the name of the file. The access argument, if present, specifies the way in which the file should be accessed. It can have any of the following values:

r    Open the file for reading only; the file must already exist. This is the default value if access is not specified.
r+    Open the file for both reading and writing; the file must already exist.
w    Open the file for writing only. If there is a file by that name, then delete all the contents of the file. If it doesn't exist, create a new file.
w+    Open the file for reading and writing. If there is a file by that name, then delete all the contents of the file. If it doesn't exist, create a new file.
a    Open the file for writing only. If the file doesn't exist, create a new empty file. Set the initial access position to the end of the file.
a+    Open the file for reading and writing. If the file doesn't exist, create a new empty file. Set the initial access position to the end of the file.


#Open the file called "jokes.txt" for writing
open "jokes.txt" w

 

puts

Syntax:
puts ?-nonewline? ?channelId? string
Use the -nonewline option only if you don't need a new line at the end of the string you write to a file. The channelID stands for the ID of the output stream that must be written to.


#Open the file called "jokes.txt" for writing
set out [open "jokes.txt" w]
puts $out "Computers make very fast, very accurate mistakes."


close

Syntax:
close ?channelId?
This command closes the channel.


#Open the file called "jokes.txt" for writing
set out [open "jokes.txt" w]
puts $out "Computers make very fast, very accurate mistakes."
close $out


After we close the file, we decide that we have to put more lines in the file. So now we open the file again, this time in the append mode.


#Open the file called "jokes.txt" for writing
set out [open "jokes.txt" w]
puts $out "Computers make very fast, very accurate mistakes."
close $out
set out [open "jokes.txt" a]
puts $out "Computers are not intelligent. They only think they are."
puts $out "My software never has bugs. It just develops random features."
puts $out {All computers wait at the same speed.
Best file compression around:  "DEL *.*" = 100% compression
DEFINITION: Computer - A device designed to speed and automate errors.
DEFINITION: Upgrade - Take old bugs out, put new ones in.}
close $out

gets

Syntax:
gets channelId ?varName?
gets will copy one line from the channel(or file) and put it in varName. If varName is not specified, the copied line will be the result of the function.


#Open the file called "jokes.txt" for writing
set out [open "jokes.txt" w]
puts $out "Computers make very fast, very accurate mistakes."
close $out
#Now append more jokes at the end of the file
set out [open "jokes.txt" a]
puts $out "Computers are not intelligent. They only think they are."
puts $out "My software never has bugs. It just develops random features."
puts $out {All computers wait at the same speed.
Best file compression around:  "DEL *.*" = 100% compression
DEFINITION: Computer - A device designed to speed and automate errors.
DEFINITION: Upgrade - Take old bugs out, put new ones in.}
close $out

seek

Syntax:
seek channelId offset ?origin?
The offset and origin arguments specify the position at which the next read or write will occur for channelId. Offset must be an integer (which may be negative) and origin must be one of the following - start, current or end.


read

Syntax:
read channelId ?numChars?
read will take numChars characters from the channel and return it. If numChars is not specified, the whole file is read and its content will be returned. You can read a file and then get all the lines into a list with every line as an item in the list. This can be done by...


set in [open "file.txt" r]
set contents [read $in]
close $in
set lines [split $contents "\n"]


Now every item in the list called $lines is a line of the file called "file.txt". On to the example - for the last time.


#Open the file called "jokes.txt" for writing
set out [open "jokes.txt" w]
puts $out "Computers make very fast, very accurate mistakes."
close $out
#Now append more jokes at the end of the file
set out [open "jokes.txt" a]
puts $out "Computers are not intelligent. They only think they are."
puts $out "My software never has bugs. It just develops random features."
puts $out {All computers wait at the same speed.
Best file compression around:  "DEL *.*" = 100% compression
DEFINITION: Computer - A device designed to speed and automate errors.
DEFINITION: Upgrade - Take old bugs out, put new ones in.}
close $out

© 2015 Thirumurugan

bottom of page