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
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.