Version 20 of eof

Updated 2012-05-07 07:41:07 by Str

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 or read, 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  

DKF: There are other conditions under which a non-blocking channel can cause a -1 to be returned by gets, notably as determined by fblocked.


The eof condition is only updated by a read, so the read loop with one conditional is:

 gets $fp line
 while {![eof $fp]} {
     # process line
     gets $fp line
 }

/Str.


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:

 package require Tcl
 package require Memchan

 set fd [open "/tmp/output.txt" "w"]
 set m [memchan]
 puts $m "someStringData"
 seek $m 0 start
 fcopy $m $fd -command CopyFinished

 proc CopyFinished args { puts $args }

 puts "Application has completed"

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 someStringData 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?