11.3 Reading from files
You can read from a file either per line or per piece.
With the gets command you can read from a file per line.
The syntax of a gets command is gets <channel> [string].
The <channel> is the channel of the file you cought with the open command.
The [string] is the string in which you want to place the retrieved line
If you specify a string the gets command will return how many characters were in the line and put the line
in the string, if you do not specify a string the line itself is returned.
A small example:
set fs [open $file r]
gets $fs line(first)
gets $fs line(second)
close $fs
This would put the first line of $file in $line(first) and the second line of $file in $line(second).
You can read a piece of the file with the read command.
The syntax of a read command is read <channel> [bytes].
The <channel> is the channel of the file you cought with the open command.
The [bytes] are the number of bytes you want to read from the file.
If you do not specify the number of bytes the whole file is read.
A small example:
set fs [open $file r]
set info [read $fs 10]
close $fs
This would put the first 10 bytes of $file into $info.
|