Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Reading in an ascii file with FOPEN()
Message
From
08/03/1999 15:51:25
 
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00194312
Message ID:
00195321
Views:
17
>>I need to read in a ascii file and do a string search on each line and
>>denpending on what is found with the string search, I will write out to another ascii file. The form will be:
>>open first ascii file
>>open second ascii file
>>do while .t.
>> read in a line of the first ascii
>> if search criteria is met
>> write out a line to the second ascii file
>> else
>> loop
>>endif
>>endo
>>
>>I will then do a APPEND FROM to bring the second ascii file into a vfp dbf. Any ideas?
>
>To do the above, you would do something like:
>
>
>
>* Method One, which you described above.
>
>procedure flImport1
>parameters pcSource, pcTarget, pcCriteria
>private all like l*
>
>lnFpRead = fopen(pcSource)
>if lnFpRead == -1
>   messagebox("Error opening source file.")
>   return(.F.)
>endif
>
>lnFpWrite = fcreate(pcTarget)
>if lnFpWrite == -1
>   = fclose(lnFpRead)
>   messagebox("Error opening target file.")
>   return(.F.)
>endif
>
>llFailed = .F.
>
>do while not feof(lnFpRead) and not llFailed
>   lcLine = fgets(lnFpRead)
>   if lower(pcCriteria) $ lower(lcLine)
>      if fputs(lnFpWrite, lcLine) == 0
>         llFailed = .T.
>      endif
>   endif
>enddo
>
>= fclose(lnFpRead)
>= fclose(lnFpWrite)
>
>* I showed above how to read and write to two files just so you know
>* how for future use. However, the better method would be to instead
>* replace the fputs() above with an INSERT INTO TABLE VALUES ( lcLine )
>* where the table would have a simple memo field for example. This would
>* accomodate very long lines where as your append from will only work
>* up to 254 character field line lengths due to limitations in VFP tables.
>*
>* If the file you are reading in has less than 254 characters/line ALWAYS,
>* then you should use Method Two below since its obviously easier.
>
>endif
>return(llFailed)
>
>
>* Method Two, faster and easier.
>
>procedure flImport2
>parameters pcSource, pcCriteria
>private all like l*
>
>create cursor import ( line char(254) )
>append from (pcSource) delimited
>delete for lower(pcCriteria) $ lower(line)
>
>* Results are already in temporary cursor tabled called "import"
>* Usage later:
>
>* scan for not deleted()
>*   ...
>*   blah blah blah
>*   ...
>*   ...
>* endscan
>
>return
>
>
>
>Please beware that I didnt test the above code. :-)
------------
Hi Grey and thanks for the code.. it really helps..:)
Robert Keith
Independent Developer
San Antonio, Texas
E-mail address:
rebelrob1@yahoo.com
Previous
Reply
Map
View

Click here to load this message in the networking platform