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
>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.
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
}
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
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
}
Here in case of ACCESS DENIED you skip the [close]. You don't want
that.
-Alex
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