Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading lines in text files... 3

Status
Not open for further replies.

mike777

Programmer
Joined
Jun 3, 2000
Messages
387
Location
US
Hello all.
In VB, there are way to read entire lines of text in flat files.
I need to count the number of lines in a text file.
I've successfully done that using the FOPEN, FREAD and then testing for line feeds. Problem is, this procedure is quite slow, since it has to read every single character in the file and then test that character.
Is there a way to just quickly determine how many lines of text are in a file?
Thank you.
-Mike
 
Hi

totLines = ALINES(laText,FILETOSTR(("cFile.txt")))

This way, array laText is obtained from cFile.txt and totLines is the number of lines in that array. Now you can scan thru thru the array and get your text vales.

FOR i = 1 to totLines
WAIT WINDOW laText(i)
ENDFOR

:-)


ramani :-)
(Subramanian.G)
 
Just remember that FileToStr() is limited to 16MB...
 
Using fgets() looks fast enough to me:


STORE FOPEN('test.txt') TO gnFileHandle && Open the file
STORE FSEEK(gnFileHandle, 0) TO gnTop && Move pointer to BOF
nlines=0
DO WHILE !FEOF(gnFileHandle)
line=FGETS(gnFileHandle,8000) && reads up to 8000 bytes
nlines=nlines+1
ENDDO
? nlines,'lines in the file'
= FCLOSE(gnFileHandle) && Close the file


Rob.
 
excellent ideas,thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top