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

Counting the number of lines in a text file VFP 1

Status
Not open for further replies.

jw1234

Technical User
Oct 3, 2010
10
GB
Anyone got any ideas using FREAD of FGETS as to how I would go about ccounting the number of lines in a text file, where each line is a different length. I do know Fox but its been a while and I just need to refresh my memory please

This would be a start fro a small project I am working on which will need further processing but if anyone can advise i'd be grateful

TIA

John
 
If the text file were less than 2GB, rather than using FREAD, I would use FILETOSTR() and bring the file into a single string.

Then from that string you could count the CRLF's by using the OCCURS() function.

If you were to use FREAD and FGETS you can use something like the following example taken directly from the VFP Help file for FGETS().

Code:
STORE FOPEN('test.txt') TO gnFileHandle    && Open the file
STORE FSEEK(gnFileHandle, 0, 2) TO gnEnd    && Move pointer to EOF
STORE FSEEK(gnFileHandle, 0) TO gnTop     && Move pointer to BOF
IF gnEnd <= 0  && Is file empty?
   WAIT WINDOW 'This file is empty!' NOWAIT
ELSE  && If not
   gcString = FGETS(gnFileHandle, gnEnd)  && Store contents
   ? gcString
ENDIF
= FCLOSE(gnFileHandle)  && Close the file

Except instead of merely printing out the line, you would count it.

Good Luck,
JRB-Bdr
 
JRB-Bldr,

First of all the given function should work, FGETS() reads a line of the text and so incrementing a counter instead of printing gcString after each FGETS would count the lines of file.

In regard to limitations:

String processing in variables only works reliable up to 16MB long strings (from the foxpro help, system cpacities topic: Maximum # of characters per character string or memory variable: 16,777,184).

You can read in more than 16MB in a string, but not all string function will work with more than 16 MB. This unfortunately is not described in more detail in the foxpro help, so I'd rather only work in chunks of data lower than 16 MB anyway.

So for files larger than 16MB you rather use low level file functions and work line by line or in chunks of eg 8KB.

Also FREAD only reads up to 2GB too, the 2GB limit applies to all file access in foxpro, low level functions are no exception to this.

But you can use OpenFile and ReadFile Windows API functions for larger files.

Bye, Olaf.
 
John,

Is there any specific reason why you must use FREAD(), etc?

If not, here's a very simple solution:

Code:
CREATE CURSOR csrCount (F1 C(1))
APPEND FROM MyTextFile.txt TYPE DELIMITED
lcCount = RECCOUNT("csrCount")
USE IN csrCount

After you run this code, lcCount will contain the number of lines in MyTextFile.txt.

I've found this considerably faster than the other techniques I've tried.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro tips, advice, training, consultancy
Custom software for your business
 
Thanks Mike and the others very handy to know I like the idea of a Cursor no there is no reason to use FREAD at all.

I am basically doing some cut and pasting of eBay feedback and at the moment editing out the information I do not want by hand. i.e Member name, Time of Transaction and the product purchased.

All I want is the Feedback itself and the date on which the transaction was done, the rest of it is totally irrelevant.

It will make my updating easier on a monthly basis, non of the so called "eBay Feedback Getters" actually work because eBay keep changing their servers, so a simple cut and paste into a text file and a little processing seems to be a reasonable answer.

Thanks john
 
Another simple solution:
Code:
CREATE CURSOR crsTest (F1 M)
INSERT INTO crsTest VALUES (FILETOSTR(GETFILE([txt])))
WAIT WINDOW [Number of lines: ] + TRANSFORM(MEMLINES(crsTest.F1))
USE IN crsTest

Testing displays the presence, not the absence of bugs.
If a software application has to be designed, it has to be designed correctly!
 
For parsing ebay mails you need to know the line number? Not really.

The most useful function besides outlook automation or mapi to get at the mails or filetostr to read in eml files will be strextract() to parse for captions or anything reoccurring.

Bye, Olaf.
 
Just want to mention that one of my favorite parsing functions is ALINES(). You can break a string into lines or pretty much anything else.

Tamar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top