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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

number of lines?

Status
Not open for further replies.

99mel

Programmer
Oct 18, 1999
379
GB
Hi everyone,

Is it possible to get the number of lines from a text file? Opening the file, looping and using 'Line Input' and adding one each time to a counter takes too long as it can be a file with half a million lines in.

Any suggestions?

Thanks in advance!

Mel s-)
 
There is no data store inside or outside of the file as to the number of lines. As a matter of fact, there really is no concept of lines IN the file system except as how software chooses to interpret CRLF.
 
Well,

John Yingling is entirely and completly correct.

On the otherhand, you can get a 'reasonable' approximation by reading some number of lines, accumulating their Lengths and dividing this by the number (of lines). The accuracy of the approximation will depend on two primary factors:

The 'sample size' (percentage of the whole) and knowing the correct "Newline" character (or at least it's length) in the file. If the file uses a single character NewLine ("CR" OR "LF") then you need to add one to the reported length of each line read. If the file includes a two character NewLine (CrLf), you need to add two to the reported length. In a LARGE file, the difference between one and two charcter "NewLine" can become significant. The "good News" is that for these same Large files the approxinmation should be within a percent or so with a sample of only 5 to 10 percent.



MichaelRed
redmsp@erols.com

There is never time to do it right but there is always time to do it over
 
Mel, if each line in your text file is the same length, you can use the following:

lngLines = FileLen("c:\YourFile.txt") / lngLineLength

FileLen() returns the size of the file in bytes.

As MichaelRed states, you need to include the CR and/or LF bytes when calculating the line length.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top