This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

This for some reason wont work

Old posts that have not been replied to for several years.
Locked
[
[Nero]
Voice
Posts: 29
Joined: Mon May 27, 2002 8:00 pm

Post by [Nero] »

This will not find the nick in the templist


set file templist
set found 0
set fs [open $file r]
while {![eof $fs]} {
gets $fs line
if {$line == $nick} { set found 1 }
}
close $fs
if {$found} {
puthelp "PRIVMSG $chan : was found!"
} else {
puthelp "PRIVMSG $chan : was not found."
}

<font size=-1>[ This Message was edited by: [Nero] on 2002-06-04 12:16 ]</font>
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

Erm, this code worked for me...

Maybe you're using different case (capital/lowercase) in the nicks? You're probably better off using "string compare -nocase" than "==".

Also, just a little nitpick, you should check for eof after gets, not before.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

No - he has done right by checking for EOF before the gets.

Remember this is a while loop.

While checking for EOF after the gets does the same thing, it will cause a empty file to have a EOF read, before it is detected.

You should check for an EOF before a read, to prevent a failed read.
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

PPSlim - No, even if you open an empty file, you will not get eof until you attempt a read. Try it.
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

yes. this is true for just about every language. eof is a function of a read statement - it gets set as a result of a failed read due to falling off the end of the file. opening a file never checks whats in it, or how long it is. It just opens the file descriptor and points to the first byte of the file (if it exists or not). It only discovers if it exists once you try to read it.
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

Beside the counting offset of one line, the code "while {![eof $fp]}" doesn't produce any errors afaik. But it should be clear that one extra empty line is processed by the code following the [gets]. This may be undesired behaviour.

One can argue that the "while {![eof $fp]}" at least makes explicit what the intention of the code is.
To make the intention explicit one could also throw in a "# read until EOF" line.

But why use EOF anyway? ...some additional info :smile:

http://mini.net/tcl/367.html

and

http://starbase.neosoft.com/~claird/com ... m.html#eof

<font size=-1>[ This Message was edited by: egghead on 2002-06-05 19:22 ]</font>
Locked