Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read text file line by line until eof

384 views
Skip to first unread message

cerr

unread,
Jan 21, 2009, 10:07:55 AM1/21/09
to
Hi there,

I would like to read single lines from a file and compare them to some
kind of input string.
I have come up with this code:
[code]
if [catch {open $barcode::OP_file r} fp] {
set barcode::status "can't open operator file:\n
$barcode::OP_file"
return
} else {
while {[eof $fp]!=1} {
gets $fp newline
delay 500
set barcode::status $newline
set barcode::scancode "Ron Eggler"
#compare currently read line from file with bar code
if {[string compare -nocase $barcode::scancode
$newline]==0} {
set barcode::status "tech validated!\nstart
testing now..."
} else {
set barcode::status "ACCESS DENIED!\nnot able to
validate $barcode::scancode!"
return
}
close $fp
lappend finaltest::operatorList $newline
}
}
[/code]
But what's happening is, that thw while {[eof $fp]!=1} doesn't seem to
be working as it's not looping at all... why cdould this be? Shouldn't
this stay in the while loop until the gets command gets an eof?
It's kinda weird...no?

Thanks for help and hints!
Ron

Jonathan Bromley

unread,
Jan 21, 2009, 10:15:59 AM1/21/09
to
On Wed, 21 Jan 2009 07:07:55 -0800 (PST), cerr wrote:

>But what's happening is, that thw while {[eof $fp]!=1} doesn't seem to
>be working as it's not looping at all... why cdould this be? Shouldn't
>this stay in the while loop until the gets command gets an eof?
>It's kinda weird...no?

Looks to me as though your [close] command is INSIDE the loop.
That feels a teeny weeny bit wrong.
--
Jonathan Bromley, Consultant

DOULOS - Developing Design Know-how
VHDL * Verilog * SystemC * e * Perl * Tcl/Tk * Project Services

Doulos Ltd., 22 Market Place, Ringwood, BH24 1AW, UK
jonathan...@MYCOMPANY.com
http://www.MYCOMPANY.com

The contents of this message may contain personal views which
are not the views of Doulos Ltd., unless specifically stated.

cerr

unread,
Jan 21, 2009, 10:26:48 AM1/21/09
to
On Jan 21, 10:15 am, Jonathan Bromley <jonathan.brom...@MYCOMPANY.com>
wrote:

> On Wed, 21 Jan 2009 07:07:55 -0800 (PST), cerr wrote:
> >But what's happening is, that thw while {[eof $fp]!=1} doesn't seem to
> >be working as it's not looping at all... why cdould this be? Shouldn't
> >this stay in the while loop until the gets command gets an eof?
> >It's kinda weird...no?
>
> Looks to me as though your [close] command is INSIDE the loop.
> That feels a teeny weeny bit wrong.
*HOOOOOOOOOOOOOPS* yep,
you're correct, right there but even after moving it out it doesn't
seem to be looping :(

if [catch {open $barcode::OP_file r} fp] {
set barcode::status "can't open operator file:\n
$barcode::OP_file"
return
} else {
while {[eof $fp]!=1} {
gets $fp newline
delay 500
set barcode::status $newline
set barcode::scancode "Ron Eggler"
#compare currently read line from file with bar code
if {[string compare -nocase $barcode::scancode
$newline]==0} {
set barcode::status "tech validated!\nstart testing
now..."
} else {
set barcode::status "ACCESS DENIED!\nnot able to
validate $barcode::scancode!"
return
}

lappend finaltest::operatorList $newline
}
close $fp
}

Bruce Hartweg

unread,
Jan 21, 2009, 10:36:20 AM1/21/09
to

are you seeing ACCESS DENIED? that branch does a return so your loop
will cease as well.

Also, you eof check is wrong - once you get your looping correct you
will find you are trying to read one more line since the eof flag
doesn't get set until the gets tries and fails to read that line.
and gets will tell you itslef if the read failed so you don;t need
eof (unless you are reading from a non-blocking the gets will return
-1, but you need to check eof or fblocked to differentiate the cause)

so try this for simplification

while {[gets $fd newline] >= 0} {
...
}

Bruce

cerr

unread,
Jan 21, 2009, 11:02:16 AM1/21/09
to

hi Bruce,

Yep thanks.... stumbled a few ti8mes but got it ironed out now. My
working solution looks like:
if {[file exists $barcode::OP_file]} {
#string compare -nocase str1 str2


if [catch {open $barcode::OP_file r} fp] {
set barcode::status "can't open operator file:\n
$barcode::OP_file"
return
} else {

while {[gets $fp newline] >= 0} {


set barcode::status $newline
set barcode::scancode "Ron Eggler"
#compare currently read line from file with bar code
if {[string compare -nocase $barcode::scancode
$newline]==0} {

set validated 1
break
}
}
# Check if the user has been validated
if {$validated} {


set barcode::status "tech validated!\nstart testing
now..."
} else {
set barcode::status "ACCESS DENIED!\nnot able to validate
$barcode::scancode!"
return
}

close $fp
}

Alexandre Ferrieux

unread,
Jan 21, 2009, 11:11:32 AM1/21/09
to

Here in case of ACCESS DENIED you skip the [close]. You don't want
that.

-Alex

schlenk

unread,
Jan 22, 2009, 5:09:09 AM1/22/09
to
On Jan 21, 4:07 pm, cerr <ron.egg...@gmail.com> wrote:
> Hi there,
>
> I would like to read single lines from a file and compare them to some
> kind of input string.

How about using Tcllib to make it easier?
http://tcllib.sourceforge.net/doc/fileutil.html

package require fileutil
fileutil::foreachLine line $filename { some script that processes the
line $line}

Michael

0 new messages