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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help NEWBIE!!! 1

Status
Not open for further replies.

GRonken

Programmer
Oct 30, 2003
26
US
I need to open a text file, read in each line, manipulate that line and return it to a text file, go get the next line etc. For example a text file needs to have each line start with "TEXT:" than the rest of the line. I am currently using SQL to do this, thought that this may be the better tool to use. I have never used TCL before ......
 
Possibly something like this?

set fidin [open <input file name> r]
set fidout [open <output file name> w]
# need to make sure we trap the EOF; we'll use a while loop
while {![eof $fidin]} {
set inline [gets $fidin]
set outline <your manipulation of $inline>
puts $fidout $outline
}
close $fidin
close $fidout

If your input file isn't too terribly big, I would read the whole thing in at once and make it a list of lines:

set fidin [open <input file name> r]
set fidout [open <output file name> w]
set linelist [split [read $fidin] \n]
close $fidin
foreach line $linelist {
set outline <your manipulation of $inline>
puts $fidout $outline
}
close $fidout



Bob Rashkin
rrashkin@csc.com
 
In the second form, it should have been

foreach inline $linelist {



Bob Rashkin
rrashkin@csc.com
 
I want to REMOVE all occurrances of the &quot; in each line of the text file ... do I -

set outline string replace ?????
 
The following works fine EXCEPT it returns an extra line with the word &quot;TEXT:&quot;. ...... any ideas?

set fidin [open C:/tcl/idxr.1 r]
set fidout [open C:/tcl/idxtest4.txt w]
# need to make sure we trap the EOF; we'll use a while loop
while {![eof $fidin]} {
set inline {}
set inline [gets $fidin]
set text &quot;TEXT:&quot;
set Compare1 [string range $inline 0 7]
set Compare2 &quot;RESULTS;&quot;

if {[string compare $Compare1 $Compare2] == 0} {
set outline $inline
} else {
append text $inline
set outline $text
}
puts $fidout $outline
}

close $fidin
close $fidout
 
Just a note on your solution, Bong. You need to be a bit careful in how you use the eof command. As it says in the reference page (emphasis added): &quot;Returns 1 if an end of file condition occurred during the most recent input operation on channelId (such as gets), 0 otherwise.&quot; In other words, eof doesn't report the EOF condition unless the EOF was detected on a previous call to gets or read. (The same applies to the behavior of the fblocked command.)

For this reason, when I read files via the gets command, I prefer the following structure:

Code:
set fid [open $file r]
while {[gets $fid line] != -1} {
    # Process the contents of the &quot;line&quot; variable
}
close $fid

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top