Hi,
I am looking to insert a couple of lines in the output. The problem is that when I try "puts $fh..." the lines are inserted only at the end of the output file and not immediately, where the last line was read...
I've attached a sample script to make more sense of my problem..... The output file which needs modification is attched after the script...
#------------ Sample script #------------ set fh [open "...123.txt" r+] while {[gets $fh line] > 0} { set temp $line if {[string eq [lindex $temp 0] "12345"]==1} { puts $fh "one two three four five" } elseif {[string eq [lindex $temp 0] "67890"]==1} { puts $fh "six seven eight nine zero" } else { } } #--#--#--#--#--
Existing output file
12345 45678 Insert the names of the above digits... ...immediately after the number 67890 90123 ...& not at the end This is the End of line. Do not print below this!!
AM (25 september 2011) This is only possible by making a copy of the file and renaming it:
set fh [open "...123.txt" r] set fhout [open "...123.txt-new" w] while {[gets $fh line] > 0} { puts $fhout $line set temp $line if {[string eq [lindex $temp 0] "12345"]==1} { puts $fhout "one two three four five" } elseif {[string eq [lindex $temp 0] "67890"]==1} { puts $fhout "six seven eight nine zero" } else { } } close $fh close $fhout file rename "...123.txt-new" "...123.txt"
This is not a limitation of Tcl - this is an operation that the operating system does not support directly. It simply cannot shift the data "downwards" - just think of a file of several millions of lines and you want to insert a new line after each original line ...