pawankalyan
pawankalyan

Reputation: 231

how to read a large file line by line using tcl?

I've written one piece of code by using a while loop but it will take too much time to read the file line by line. Can any one help me please? my code :

   set a [open myfile r]              
   while {[gets $a line]>=0} {   
     "do somethig by using the line variable" 
   }

Upvotes: 8

Views: 51271

Answers (2)

Todd Horst
Todd Horst

Reputation: 863

If it truly is a large file you should do the following to read in only a line at a time. Using your method will read the entire contents into ram.

https://www.tcl.tk/man/tcl8.5/tutorial/Tcl24.html

#
# Count the number of lines in a text file
#
set infile [open "myfile.txt" r]
set number 0

#
# gets with two arguments returns the length of the line,
# -1 if the end of the file is found
#
while { [gets $infile line] >= 0 } {
    incr number
}
close $infile

puts "Number of lines: $number"

#
# Also report it in an external file
#
set outfile [open "report.out" w]
puts $outfile "Number of lines: $number"
close $outfile

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137567

The code looks fine. It's pretty quick (if you're using a sufficiently new version of Tcl; historically, there were some minor versions of Tcl that had buffer management problems) and is how you read a line at a time.

It's a little faster if you can read in larger amounts at once, but then you need to have enough memory to hold the file. To put that in context, files that are a few million lines are usually no problem; modern computers can handle that sort of thing just fine:

set a [open myfile]
set lines [split [read $a] "\n"]
close $a;                          # Saves a few bytes :-)
foreach line $lines {
    # do something with each line...
}

Upvotes: 11

Related Questions