Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TCL, read in specific line of input

1,360 views
Skip to first unread message

Lim kiang Leng

unread,
May 5, 2004, 4:51:03 PM5/5/04
to
Can any 1 help me to read in a specific Line of input from the text file.
such as: Do tcl have a build in command to perform the task below?


set spofile [open test.txt a+]

while { [gets $spofile line] >= 0 } {
incr line_no
if { $line_no == 4 } {

puts " i have read in line 4"
}
}

Bryan Oakley

unread,
May 5, 2004, 5:24:44 PM5/5/04
to
Lim kiang Leng wrote:

That's the way to do it.

Asif

unread,
May 5, 2004, 10:13:08 PM5/5/04
to
Bryan Oakley <br...@bitmover.com> wrote in message news:<wYcmc.5598$3Y6....@newssvr24.news.prodigy.com>...

or

set line_no 4


set spofile [open test.txt a+]

for {set i 1} {$i <= $line_no} {incr i} { gets $spofile line ; }

Donal K. Fellows

unread,
May 6, 2004, 6:20:50 AM5/6/04
to
Asif wrote:
> or
> set line_no 4
> set spofile [open test.txt a+]
> for {set i 1} {$i <= $line_no} {incr i} { gets $spofile line ; }
> puts "i have read in line 4"

That's not so good if you have a file with only three lines in it. :^)
Lim's solution is best for text files. You can do better than that if
you have files where every line is the same length, since then you can
use [seek] to go to exactly the right spot, but that doesn't work with
variable-length records (or not without a separate index file, which
makes everything quite a bit more complex.)

Donal.

Bryan Schofield

unread,
May 6, 2004, 11:24:43 PM5/6/04
to
lvi...@yahoo.com wrote:
> According to Donal K. Fellows <donal.k...@man.ac.uk>:
> Or one could read in the entire file into a string, and split the
> string at newlines, then pick up the appropriate list entry.
>
> But the bottom line is that many of the current operating systems do not
> provide a function to retrieve a specific line of a text file.


Just so everyone knows, this question was asked in couple of places so
to consolidate the different answers:

http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=LUimc.530005%24B81.9584946%40twister.tampabay.rr.com&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.lang.tcl

http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=c7df4q%24lfh%243%40srv38.cas.org&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.lang.tcl

http://groups.google.com/groups?dq=&start=25&hl=en&lr=&ie=UTF-8&oe=UTF-8&group=comp.lang.tcl&selm=4282e185.0405050812.cd1450f%40posting.google.com

0 new messages