Version 0 of read

Updated 2001-05-28 15:40:58

read - Read characters from a channel

  read channelId ?number-of-characters?

Reads a sequence of characters from the specified channel. The read characters are returned as the result of the command.

If a number-of-characters was specified that many characters will be read (except if EOF is encountered first). Without an exact number of characters to read the command will read everything until it encounters EOF. The just described behaviour is true only if the channel is blocking. In the case of a non-blocking channel the command will try its best to get the specified number of characters, but will bail out if the internal buffers of Tcl are empty and the underlying OS structure incapable of delivering more input. When this happens the command will just return the characters it could read.

When Working with binary data make sure the channel was configured with -translation binary. Only then it is allowed to equate characters and bytes.

As is said in tcllib, it's better to "use the [file size] command to get the size, which preallocates memory, rather than trying to grow it as the read progresses."

 read channelId [file size $_filename]

rather than just

 read channelId

Or use the fileutil::cat command instead where this is implemented.


Tcl syntax help