Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inserting a line in between a file and not in the end

Status
Not open for further replies.

eugene42

Programmer
Sep 25, 2011
6
US

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!!
 
To do such task, with any programming language, you need to open a temporary file onto which to write what you want, then you rename the temporary file to the original one, deleting the latter.

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top