This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Reading a certain line of a text file....

Old posts that have not been replied to for several years.
Locked
J
Jimbo

Post by Jimbo »

Without doing gets $channel line 5 times....how can i read the 5th line of a file?
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

There is no way. Allthough you could create a proc that will do the the gets 5 times for you.

Code: Select all

proc get_line {file line} {
  set fp [open $file r]
  set r {}
  while {![eof $fp]} {
    set i 0
    while {$i < $line} {
      set r [gets $fp]
      incr i
    }
    break
  }
  catch {close $fp}
  return $r
}
Then you can use it like

Code: Select all

my code here
set test [get_line file.name 5]
which will get line 5
W
Wcc
Master
Posts: 278
Joined: Sun Oct 28, 2001 8:00 pm
Location: USA
Contact:

Post by Wcc »

It would make more sense to me to read the entire file with [read $channel], split it by n, then use lindex to pull lines.
P
Petersen
Owner
Posts: 685
Joined: Thu Sep 27, 2001 8:00 pm
Location: Blackpool, UK

Post by Petersen »

well, ppslim's way is cpu intensive, your way is ram intensive. which method is the best to use depends on the length of the file.
J
Jimbo

Post by Jimbo »

Cheers ppl...Just so ya know im using ppslim's idea cos its neater....cos im needing to get a certain line quite frequently!!!
Locked