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.

Reading arguments from a file

Old posts that have not been replied to for several years.
Locked
a
anzuhan

Post by anzuhan »

I'm trying to delete a line from a file with a specified argument. Ie. if the var name would be "[censored]" and it searches for from the file the line and clears it. anyway, this doesn't work. I tried to read from a file a code with this code :

set filename "test.dat"
set file [open $filename]
set varname "vartofind"

while {![eof $file]} {
set line [gets $file]
if {[lindex $line 0] == $varname]} {
puts $file ""
}
}
close $file

Hope somebody knows :smile:
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

There are several problems with your script.

First, you shouldn't use [lindex $line 0] unless you're very sure about the file structure, because if it has a special char (eg. {, [), you will have a tcl error.

Second, you shouldn't test for eof until after you attempt to read from the file. Eof isn't reported until you try to read past the file. So make it something like this:
while {[gets $file line] >= 0} {
...
}

Finally, you can't delete a line from the file by doing puts $file "". What you have to do is read in the entire file, re-open the file in overwrite mode, and then write out everything you don't want deleted.
Locked