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

Text Files

Status
Not open for further replies.

ching0418

MIS
Mar 6, 2002
96
HK
hello friends,

i just want to ask what function can i use to check the number of lines or records of a text file. Some functions like EOF(file),LOC(file), etc.

TIA,
ching
 
The following recent thread should get you started

thread222-404019 To get what you want, you have to go through the crap, but to get through the crap you have to know what you want... [rockband]
 
thanks for that. i've checked the thread. but what i need is something like a function which will get the number of rows without looping through the lines because i'll be using it for my progress bar, to get the percentage of every bar increment during process looping
 
Tempstr = Space$(3000)
LF = Chr$(10)
Open InputFile For Binary As #1
Get #1, , Tempstr
RecordSize = InStr(1, Tempstr, LF)
Close #1
Open InputFile For Binary As #1
NumOfRecords = LOF(1) \ RecordSize
Close #1 Swi
 
To my knowledge, with a text file it is not possible to get the number of rows with actually reading the entire file because the file has no fixed format. In other words, you don't know how long any individual line is until you actually read that line.

It is possible however, if you know a typical, or average line length, to approximate the line count by using the LOF (length of file) function.

By dividing the results of the LOF by the typical line length (and I would add 1 more), you should have a reasonable estimate
of the number of lines. It won't always be exact, but it may very well suffice for a progress bar. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CajunCenturion,

I agree with you here. For something like a progress bar this type of estimation is often required. In a case like this I might even read the 1st few lines of data to estimate the average length if I have no way to know what it'll be.

This can still make things way off, but so be it.

The Swi approach will work great for teeny files, but I deal with a lot of files in the multi-megabyte range, which is why I use progress bars anyhow. The processing takes awhile and I want to show the user progress feedback.
 
For teeny files??? My approach will return the size of a file up to 2 gig in size. This is because of VB's limiting LOF function. I work with flat files almost exclusively on a day to day basis and this is the way I determine how many records I am dealing with. My usual file size ranges from anywhere between 50 mb to over 2 gig. If you want to return the file size of a file larger than 2 gig you may want to look into the GetFileSize API. Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top