Reply-To: erik.wachtmeester@bighole.iaf.nl
Dirk Stuijfzand wrote in a message to Ben Cavanagh:
DS> Create a temp DBF with one CHAR field, lenght >= longest line length
DS> in txt file. Example: TEXT.DBF, field LINE, type C, LEN 80
DS> USE TEXT
DS> ZAP
DS> APPE FROM text.doc SDF
DS> This is very simple, even works in dBaseIII! Now you have a DBF where
DS> every line from the textfile is a record in TEXT.DBF.
[...]
Nice solution! But intuitively I would opt for a more low level approach
(using
more code) instead of this temp DBF approach. Maybe because I did a lot of
Delphi and C++ coding lately? ;-)
Something like this (off the top of my hat, and definitely not tested!):
<----------
#define END_OF_LINE chr(13) + chr(10)
#define BLOCKSIZE 8192
local nHandle, nPointer, cLine, cBuffer
local cRemainder := ''
local lContinue := .t.
// open PERSON table exclusively
dbusearea( .t.,, 'PERSON.DBF',, .f. )
// open textfile in readonly mode
nHandle := fopen( 'text.doc', 0 )
if ( nHandle != -1 )
// textfile opened successfully, so we're ready to go
do while ( lContinue )
cBuffer := freadstr( nHandle, BLOCKSIZE )
lContinue := ( len( cBuffer ) + len( cRemainder ) > 0 )
// dummy value to initialise the following do while loop
nPointer := -1
// get individual lines from text buffer and process them
do while ( nPointer != 0 )
nPointer := at( END_OF_LINE, cBuffer )
if ( nPointer != 0 ) .or. !empty( cRemainder )
cLine := cRemainder + left( cBuffer, nPointer - 1 )
cBuffer := substr( cBuffer, nPointer + len( END_OF_LINE ) - 1 )
// clear possible remainder, because it's parsed already
cRemainder := ''
// handle text to field conversions here
dbappend()
fieldput( fieldpos( 'NAME' ), substr( cLine, 1, 30 ) )
fieldput( fieldpos( 'PHONE' ), substr( cLine, 60, 15 ) )
else
// save possible remainder of text buffer
cRemainder := cBuffer
endif
enddo
enddo
fclose( nHandle )
else
// error opening textfile
endif
// close PERSON table
dbclosearea()
---------->
I really do like your solution, but since I started thinking about my
olution
already, I thought I'd add it to the thread. For better or for worse... ;-)
My solution uses a 'little bit' more code, but when converted to a library
routine you can reuse it easily, and with a few changes it can be converted
o
reading from e.g. a comport, etc. instead of a textfile.
Regards,
Erik
--- ifmail v.2.13-tx8.7
---------------
* Origin: May it be on this earth? (2:283/7.2)
|