Version 11 of eof

Updated 2003-09-24 19:23:00

eof - Check for end of file condition on channel

eof channelId

Returns 1 if an end of file condition occurred during the most recent input operation on channelId (such as gets), 0 otherwise.

ChannelId must be an identifier for an open channel such as a Tcl standard channel (stdin, stdout, or stderr), the return value from an invocation of open or socket, or the result of a channel creation command provided by a Tcl extension.


eof is, in one sense, a command:

http://purl.org/tcl/home/man/tcl8.4/TclCmd/eof.htm

and, in another sense, an acronym for End Of File.


Programmers can use eof to determine whether the channel has reached the end of input.

Note that it fires only after the last successful gets, so testing for a -1 return value on gets may be a better idea, and avoid eof's FMM [L1 ] by coding:

 while {[gets $fp line]>=0} {...}

instead of

 while {![eof $fp]} {
    gets $fp line
    if [eof $fp break] ;# otherwise loops one time too many
    ...
 } ;# RS  

Hmmm... the only thing I find eof good for is as a condition which should trigger closing a socket.

That is, if you are trying to read a non-blocking socket and you hit eof, you KNOW that it's time to close the socket.

-PSE


Can anyone indicate whether there are certain types of channels (pipe, socket, etc.) where eof doesn't do what one might at first blush expect?

JMN 2003-07-15 This isn't directly referring to the eof command - but is an EOF issue that I wasn't expecting so here looks as good a place as any to mention it.

Using the Memchan package, I was trying to fcopy from a memchan to a file roughly like so:

 set m [memchan]
 puts $m "someStringData"
 seek $m 0 start
 fcopy $m $fd -command CopyFinished

 proc CopyFinished args { ... }

This performed the copy to file just fine, but the CopyFinished proc never got called. To fix this, I called

 fconfigure $m -eofchar {}

before the puts line.. and

 fconfigure $m -eofchar \x1a

before the fcopy line. Then I changed the puts line to:

 puts $m "someStringData\x1a"

This seems like a bit of hoop-jumping to get the expected fcopy behaviour.. am I missing something simple?


Tcl syntax help - Arts and Crafts of Tcl-Tk Programming - Category Command - ]Category Introspection]