Page 1 of 1 1
Topic Options
#195430 - 2009-08-15 06:17 AM Reading a specific line from a file
lxadmin Offline
Just in Town

Registered: 2009-08-15
Posts: 2
Loc: kol
Hi everyone,
I am trying to write a script that can read the three words that the text file contain. The contents of the text file looks like :
one#
two#
three#

With the following script I can get the three strings from the text file:
 Code:
Break On
IF Open(3,"filepath")=0
  $line = ReadLine(3)
  WHILE @ERROR = 0
    ? $line
    $line = ReadLine(3)
  LOOP
  Close (3)
EndIf
Sleep 5


The output of this script is:
one#
two#
three#

What I want is to read only the 2nd line of the file. That is the output will be only -
Two#

rather than having

one#
two#
three#

Please help.


Edited by Allen (2009-08-15 06:43 AM)
Edit Reason: added code tags

Top
#195431 - 2009-08-15 06:59 AM Re: Reading a specific line from a file [Re: lxadmin]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
One way using your code, slightly modified...

 Code:
Break On
$ffh=freefilehandle()
IF Open($ffh,"filepath")=0
  $line = ReadLine($ffh)
  WHILE @ERROR = 0
    $count=$count+1
    if $count=2
      ? $line
    endif
    $line = ReadLine($ffh)
  LOOP
  $RC=Close($ffh)
EndIf
Sleep 5


Another way, using Readfile() - http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=84660

 Code:
$filecontents=readfile("filepath") ;puts the contents of the file in an array
? $filecontents[1] ; arrays are zero based, so to see line 2 subtract 1 = 1 and put in the square brackets

;paste the linked function here/or the bottom of your script




Edited by Allen (2009-08-15 12:38 PM)

Top
#195432 - 2009-08-15 08:56 AM Re: Reading a specific line from a file [Re: Allen]
lxadmin Offline
Just in Town

Registered: 2009-08-15
Posts: 2
Loc: kol
Thanks...both the codes are working fine for me.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 781 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.052 seconds in which 0.022 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org