And I thought people are weird for putting a lime into beers. Here's how to reverse the process.
function TabIn
local i, cData, cString, nCount, nFieldCount, nFileSize, nInHandle
nInHandle := fopen( 'tabfile.txt' )
nFileSize := fseek( nInHandle, 0, 2 ) // Find file size
fseek( nInHandle, 0, 0 )
use inv846 //this is my dbf file
go top
nFieldCount := fcount()
**-- Loop until end of file
do while ( fseek( nInHandle, 0, 1 ) < nSize )
cString := readln( nInHandle )
nCount := 1
**-- There's data to add
if ( len( cString ) > 0 )
append blank
rlock()
endif
**-- Loop to parse each line
do while ( len( cString ) > 0 )
nPos := at( chr( 9 ), cString ) // Check for a TAB
if ( nPos == 0 )
exit
endif
cData := substr( cString, 1, nPos - 1 )
**-- Convert data to proper field format
if ( valtype( fieldget( nCount ) ) == 'M' ) // Memo Field
elseif ( valtype( fieldget( nCount ) ) == 'D' ) // Date Field
cData := stod( cData )
elseif ( valtype( fieldget( nCount ) ) == 'N' ) // Numeric Field
cData := val( cData )
elseif ( valtype( fieldget( nCount ) ) == 'L' ) // Logical Field
cData := iif( cData == '1', .t., .f. )
endif
fieldput( nCount, cData )
nCount++
**-- Check for too much data on a line
if ( nCount > nFieldCount )
exit
endif
**-- Reposition string
cString := substr( cString, nPos + 1 )
enddo
unlock
enddo
fclose( nInHandle )
close
return ( .t. )
You'll also need this function.
function readln ( nHandle )
local cString, nPos, ;
cReturn := NULL
do while ( TRUE )
if ( len( ( cString := freadstr( nHandle, 256 ) ) ) == 0 )
exit
elseif ( ( nPos := at( chr( 10 ) + chr( 13 ), cString ) ) > 0 )
cReturn += left( cString, nPos - 1 )
if ( nPos < len( cString ) - 1 )
fseek( nHandle, - ( len( cString ) - nPos - 1 ), 1 )
endif
exit
endif
cReturn += cString
enddo
return ( cReturn )
NOTE: There are three assumptions at work here. 1) Dates are stored in the TAB file as YYYYMMDD. 2) The data in the TAB file is in the same order as the fields in the database. 3) No memo fields will be filled in from a TAB file.