Normal Topic ReadLn number (Read 293 times)
obfusc88
Full Member
***
Offline


No personal text

Posts: 194
Joined: Dec 17th, 2005
ReadLn number
Dec 16th, 2007 at 3:13am
Print Post Print Post  
I have three questions about using FileReadLn.  I have shown a possible answer for the first question.

1.  How can I determine how many lines are in the file I am opening? 
Code
Select All
// Count lines in file
var vFileHandle as Int
var vFileSize as Int
var vLineContent as String
var vLines as Int

vFileHandle = FileOpen("C:\Test\Logfile.txt")
vFileSize = FileSize(vFileHandle)

FileSeek(vFileHandle,0)
While(FilePos(vFileHandle) < vFileSize) {
	FileReadLn(vFileHandle,vLineContent)
	vLines = vLines + 1
	  }
FileClose(vFileHandle)
WriteLn("File has " + vLines + " lines.")
 



2.  How can I read the last line when each time the file may have a different number of lines?

3.  How can I read line "n", then read line n+2, and then read line n+7?

The FileReadLn function only shows the file source and the destination.
FileSeek gets me to a position in a file, but how to know the position that starts a particular line?
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: ReadLn number
Reply #1 - Dec 16th, 2007 at 3:39pm
Print Post Print Post  
You have already answered your own questions.

1.  The code you have for question 1 looks good.  Is it not working for you?

3.  You can use the same code in question 1 to read line "n".  Just add an IF statement.  If vLines = n then vLineContent has the value of that line
While(FilePos(vFileHandle) < vFileSize) {
     FileReadLn(vFileHandle,vLineContent)
     vLines = vLines + 1
     If vLines = n Then {
              Do something with vLineContent
              }

        }

2.  And now you can read the last line by running both in series.  Run first code to get line count (vLines).  Set n=vLines.  Run second code looping until n = Linecount
------------------------------------------
n = vLines
vLines=0
FileSeek(vFileHandle,0)
While(vLines < n) {
     FileReadLn(vFileHandle,vLineContent)
     vLines = vLines + 1
     }
Do something with vLineContent
------------------------------------------

Note, samples above are not tested.      
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged