creating DBF file from text file

Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

creating DBF file from text file

Post by Ehab Samir Aziz »

Dear all,
I need to store pages of text file delimited by FORMFEED character to Memo fields .
each record is indexed by unique key such account number .
Where can I find functions that deal with this matter ?
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

Can you post a small file so we can run some tests?
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

storing each page to a memo field and the account number to a separate field and then reindex the DBF according to account number. Then writing the indexed result to a new text file.


page 1 :Account number :444
..
..
Total : 1100
page 2 :Account number :444
..
..
Total : 1100
page 3 :Account number :444
..
..
Total : 1100
page 4 :Account number :444
..
..
Total : 1100
page 5 :Account number :444
..
..
Total : 1100
page 6 :Account number :444
..
..
Total : 1100
page 7 :Account number :444
..
..
Total : 1100
page 1 :Account number :445
..
..
Total : 1300
page 2 :Account number :445
..
..
Total : 1300
page 3 :Account number :445
..
Total : 1300
%%EOF
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

Do the actual text file have the markers "Account number :" and "Total :" ? If yes it´s easy.
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

Could you please draw a clue about the syntax . The markers are the line number and position number of each account number(444,445). The delimited between each page is formfeed .
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

It might be something like this:

Code: Select all

PROCEDURE FillDB
LOCAL oFile := TFileRead():New( "YOUR_FILE.TXT" )
LOCAL cTemp := ""
LOCAL cAcNb := ""

CreateDBF()

oFile:Open()
IF oFile:Error()
    msgStop( oFile:ErrorMsg( "FileRead: " ) )
ELSE
    WHILE oFile:MoreToRead()
        cTemp := oFile:ReadLine()
        IF "Account number" $ cTemp
            IF cAcNb != padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 ) // New Account
                cAcNb := padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 )
                test->( dbAppend() )
                test->ACNB := cAcNb
            ENDIF
        ELSE
            IF !"Total:" $ cTemp .AND. !"EOF:" $ cTemp
                test->TEXT += ( " " + allTrim( cTemp ) )
            ENDIF
        ENDIF
    ENDDO
    oFile:Close()
ENDIF
test->( dbCloseArea() )
RETURN

STATIC PROCEDURE CreateDBF()
LOCAL aStru := {}

aAdd( aStru, { "ACNB", "C",  8, 0 } )
aAdd( aStru, { "TEXT", "M", 10, 0 } )
dbCreate( "TEST", aStru,, .T., "test" )
RETURN
!tested :)
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

My code was as below but with the error
Error: Unresolved external '_HB_FUN_TFILEREAD' referenced from F:\PROGRAMS\CLIPP
ER\FWH\TDATA\DOCUCOLLECT\MAIN.OBJ

Code: Select all

#include "fivewin.ch"


external ordkeyno, ordkeycount, ordKeygoto

function main()
*--------------------------

fillDB()
   
return nil

PROCEDURE FillDB
LOCAL oFile := TFileRead():New( "all.txt101_data_P.txt" )
LOCAL cTemp := ""
LOCAL cAcNb := ""

CreateDBF()

oFile:Open()
IF oFile:Error()
    msgStop( oFile:ErrorMsg( "FileRead: " ) )
ELSE
    WHILE oFile:MoreToRead()
        cTemp := oFile:ReadLine()
        IF "Account number" $ cTemp
            IF cAcNb != padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 ) // New Account
                cAcNb := padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 )
                test->( dbAppend() )
                test->ACNB := cAcNb
            ENDIF
        ELSE
            IF !"Total:" $ cTemp .AND. !"EOF:" $ cTemp
                test->TEXT += ( " " + allTrim( cTemp ) )
            ENDIF
        ENDIF
    ENDDO
    oFile:Close()
ENDIF
test->( dbCloseArea() )
RETURN

STATIC PROCEDURE CreateDBF()
LOCAL aStru := {}

aAdd( aStru, { "ACNB", "C",  8, 0 } )
aAdd( aStru, { "TEXT", "M", 10, 0 } )
dbCreate( "TEST", aStru,, .T., "test" )
RETURN

 
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

You have to link the hbmisc lib from harbour.
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

My harbour version is :
Harbour 1.0.0 Intl. (Rev. 9201)
Copyright (c) 1999-2008, http://www.harbour-project.org/

I can not find that library in \harbour\lib

ace32.lib
adordd.lib
ads32.lib
codepage.lib
common.lib
dbfcdx.lib
dbffpt.lib
dbfntx.lib
debug.lib
gtgui.lib
gtwin.lib
hbcommon.lib
hbcpage.lib
hbdebug.lib
hblang.lib
hbmacro.lib
HbOle.lib
hboleaut.lib
hbpp.lib
hbrdd.lib
hbrtl.lib
hbsix.lib
hbusrrdd.lib
hbvm.lib
hbw32.lib
hbziparch.lib
hsx.lib
lang.lib
macro.lib
pp.lib
rdd.lib
rddado.lib
rddads.lib
rddcdx.lib
rddfpt.lib
rddntx.lib
rtl.lib
usrrdd.lib
vm.lib
~GLH0074.TMP
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

Because you are using a very old Harbour version. If you can´t or do not want to upgrade you can try to link the class to your app.

Code: Select all

/*
 * $Id: fileread.prg 17867 2012-07-21 16:48:00Z vszakats $
 */

/* Harbour Project source code
 * A class that reads a file one line at a time
   http://harbour-project.org/
   Donated to the public domain on 2001-04-03 by David G. Holm <dholm@jsd-llc.com>
*/

#include "hbclass.ch"

#include "fileio.ch"

#define oF_ERROR_MIN          1
#define oF_CREATE_OBJECT      1
#define oF_OPEN_FILE          2
#define oF_READ_FILE          3
#define oF_CLOSE_FILE         4
#define oF_ERROR_MAX          4
#define oF_DEFAULT_READ_SIZE  4096

CREATE CLASS TFileRead

   VAR cFile                   // The filename
   VAR nHan                    // The open file handle
   VAR lEOF                    // The end of file reached flag
   VAR nError                  // The current file error code
   VAR nLastOp                 // The last operation done (for error messages)
   VAR cBuffer                 // The readahead buffer
   VAR nReadSize               // How much to add to the readahead buffer on
                               // each read from the file

   METHOD New( cFile, nSize )  // Create a new class instance
   METHOD Open( nMode )        // Open the file for reading
   METHOD Close()              // Close the file when done
   METHOD ReadLine()           // Read a line from the file
   METHOD Name()               // Retunrs the file name
   METHOD IsOpen()             // Returns .T. if file is open
   METHOD MoreToRead()         // Returns .T. if more to be read
   METHOD Error()              // Returns .T. if error occurred
   METHOD ErrorNo()            // Returns current error code
   METHOD ErrorMsg( cText )    // Returns formatted error message

   PROTECTED:

   METHOD EOL_pos()

END CLASS

METHOD New( cFile, nSize ) CLASS TFileRead

   IF nSize == NIL .OR. nSize < 1
      // The readahead size can be set to as little as 1 byte, or as much as
      // 65535 bytes, but venturing out of bounds forces the default size.
      nSize := oF_DEFAULT_READ_SIZE
   ENDIF

   ::cFile     := cFile             // Save the file name
   ::nHan      := -1                // It's not open yet
   ::lEOF      := .T.               // So it must be at EOF
   ::nError    := 0                 // But there haven't been any errors
   ::nLastOp   := oF_CREATE_OBJECT  // Because we just created the class
   ::cBuffer   := ""                // and nothing has been read yet
   ::nReadSize := nSize             // But will be in this size chunks

   RETURN Self

METHOD Open( nMode ) CLASS TFileRead

   IF ::nHan == -1
      // Only open the file if it isn't already open.
      IF nMode == NIL
         nMode := FO_READ + FO_SHARED   // Default to shared read-only mode
      ENDIF
      ::nLastOp := oF_OPEN_FILE
      ::nHan := FOPEN( ::cFile, nMode )   // Try to open the file
      IF ::nHan == -1
         ::nError := FERROR()       // It didn't work
         ::lEOF   := .T.            // So force EOF
      ELSE
         ::nError := 0              // It worked
         ::lEOF   := .F.            // So clear EOF
      ENDIF
   ELSE
      // The file is already open, so rewind to the beginning.
      IF FSEEK( ::nHan, 0 ) == 0
         ::lEOF := .F.              // Definitely not at EOF
      ELSE
         ::nError := FERROR()       // Save error code if not at BOF
      ENDIF
      ::cBuffer := ""               // Clear the readahead buffer
   ENDIF

   RETURN Self

METHOD ReadLine() CLASS TFileRead
   LOCAL cLine := ""
   LOCAL nPos

   ::nLastOp := oF_READ_FILE

   IF ::nHan == -1
      ::nError := -1                // Set unknown error if file not open
   ELSE
      // Is there a whole line in the readahead buffer?
      nPos := ::EOL_pos()
      WHILE ( nPos <= 0 .OR. nPos > LEN( ::cBuffer ) - 3 ) .AND. !::lEOF
         // Either no or maybe, but there is possibly more to be read.
         // Maybe means that we found either a CR or an LF, but we don't
         // have enough characters to discriminate between the three types
         // of end of line conditions that the class recognizes (see below).
         cLine := FREADSTR( ::nHan, ::nReadSize )
         IF EMPTY( cLine )
            // There was nothing more to be read. Why? (Error or EOF.)
            ::nError := FERROR()
            IF ::nError == 0
               // Because the file is at EOF.
               ::lEOF := .T.
            ENDIF
         ELSE
            // Add what was read to the readahead buffer.
            ::cBuffer += cLine
         ENDIF
         // Is there a whole line in the readahead buffer yet?
         nPos := ::EOL_pos()
      END WHILE
      // Is there a whole line in the readahead buffer?
      IF nPos <= 0
         // No, which means that there is nothing left in the file either, so
         // return the entire buffer contents as the last line in the file.
         cLine := ::cBuffer
         ::cBuffer := ""
      ELSE
         // Yes. Is there anything in the line?
         IF nPos > 1
            // Yes, so return the contents.
            cLine := LEFT( ::cBuffer, nPos - 1 )
         ELSE
            // No, so return an empty string.
            cLine := ""
         ENDIF
         // Deal with multiple possible end of line conditions.
         DO CASE
            CASE SUBSTR( ::cBuffer, nPos, 3 ) == CHR( 13 ) + CHR( 13 ) + CHR( 10 )
               // It's a messed up DOS newline (such as that created by a program
               // that uses "\r\n" as newline when writing to a text mode file,
               // which causes the '\n' to expand to "\r\n", giving "\r\r\n").
               nPos += 3
            CASE SUBSTR( ::cBuffer, nPos, 2 ) == CHR( 13 ) + CHR( 10 )
               // It's a standard DOS newline
               nPos += 2
            OTHERWISE
               // It's probably a Mac or Unix newline
               nPos++
         ENDCASE
         ::cBuffer := SUBSTR( ::cBuffer, nPos )
      ENDIF
   ENDIF

   RETURN cLine

METHOD EOL_pos() CLASS TFileRead
   LOCAL nCRpos, nLFpos, nPos

   // Look for both CR and LF in the file read buffer.
   nCRpos := AT( CHR( 13 ), ::cBuffer )
   nLFpos := AT( CHR( 10 ), ::cBuffer )
   DO CASE
      CASE nCRpos == 0
         // If there's no CR, use the LF position.
         nPos := _ nLFpos == 0
         // If there's no LF, use the CR position.
         nPos := nCRpos
      OTHERWISE
         // If there's both a CR and an LF, use the position of the first one.
         nPos := MIN( nCRpos, nLFpos )
   ENDCASE

   RETURN nPos

METHOD Close() CLASS TFileRead

   ::nLastOp := oF_CLOSE_FILE
   ::lEOF := .T.
   // Is the file already closed.
   IF ::nHan == -1
      // Yes, so indicate an unknown error.
      ::nError := -1
   ELSE
      // No, so close it already!
      FCLOSE( ::nHan )
      ::nError := FERROR()
      ::nHan   := -1                // The file is no longer open
      ::lEOF   := .T.               // So force an EOF condition
   ENDIF

   RETURN Self

METHOD Name() CLASS TFileRead
   // Returns the filename associated with this class instance.
   RETURN ::cFile

METHOD IsOpen() CLASS TFileRead
   // Returns .T. if the file is open.
   RETURN ::nHan != -1

METHOD MoreToRead() CLASS TFileRead
   // Returns .T. if there is more to be read from either the file or the
   // readahead buffer. Only when both are exhausted is there no more to read.
   RETURN !::lEOF .OR. !EMPTY( ::cBuffer )

METHOD Error() CLASS TFileRead
   // Returns .T. if an error was recorded.
   RETURN ::nError != 0

METHOD ErrorNo() CLASS TFileRead
   // Returns the last error code that was recorded.
   RETURN ::nError

METHOD ErrorMsg( cText ) CLASS TFileRead
   STATIC s_cAction := {"on", "creating object for", "opening", "reading from", "closing"}

   LOCAL cMessage, nTemp

   // Has an error been recorded?
   IF ::nError == 0
      // No, so report that.
      cMessage := "No errors have been recorded for " + ::cFile
   ELSE
      // Yes, so format a nice error message, while avoiding a bounds error.
      IF ::nLastOp < oF_ERROR_MIN .OR. ::nLastOp > oF_ERROR_MAX
         nTemp := 1
      ELSE
         nTemp := ::nLastOp + 1
      ENDIF
      cMessage := iif( EMPTY( cText ), "", cText ) + "Error " + hb_ntos( ::nError ) + " " + s_cAction[ nTemp ] + " " + ::cFile
   ENDIF

   RETURN cMessage
 
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

I made the file main.prg as below but I got the error :

Error: Unresolved external '_HB_FUN_HB_NTOS' referenced from F:\PROGRAMS\CLIPPER
\FWH\TDATA\DOCUCOLLECT\MAIN.OBJ
* There are errors

Code: Select all

#include "fivewin.ch"
#include "hbclass.ch"
#include "fileio.ch"


external ordkeyno, ordkeycount, ordKeygoto

function main()
*--------------------------

fillDB()
   
return nil

PROCEDURE FillDB
LOCAL oFile := TFileRead():New( "all.txt101_data_P.txt" )
LOCAL cTemp := ""
LOCAL cAcNb := ""

CreateDBF()

oFile:Open()
IF oFile:Error()
    msgStop( oFile:ErrorMsg( "FileRead: " ) )
ELSE
    WHILE oFile:MoreToRead()
        cTemp := oFile:ReadLine()
        IF "Account number" $ cTemp
            IF cAcNb != padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 ) // New Account
                cAcNb := padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 )
                test->( dbAppend() )
                test->ACNB := cAcNb
            ENDIF
        ELSE
            IF !"Total:" $ cTemp .AND. !"EOF:" $ cTemp
                test->TEXT += ( " " + allTrim( cTemp ) )
            ENDIF
        ENDIF
    ENDDO
    oFile:Close()
ENDIF
test->( dbCloseArea() )
RETURN

STATIC PROCEDURE CreateDBF()
LOCAL aStru := {}

aAdd( aStru, { "ACNB", "C",  8, 0 } )
aAdd( aStru, { "TEXT", "M", 10, 0 } )
dbCreate( "TEST", aStru,, .T., "test" )
RETURN

/*
 * $Id: fileread.prg 17867 2012-07-21 16:48:00Z vszakats $
 */

/* Harbour Project source code
 * A class that reads a file one line at a time
   http://harbour-project.org/
   Donated to the public domain on 2001-04-03 by David G. Holm <dholm@jsd-llc.com>
*/


#define oF_ERROR_MIN          1
#define oF_CREATE_OBJECT      1
#define oF_OPEN_FILE          2
#define oF_READ_FILE          3
#define oF_CLOSE_FILE         4
#define oF_ERROR_MAX          4
#define oF_DEFAULT_READ_SIZE  4096

CREATE CLASS TFileRead

   VAR cFile                   // The filename
   VAR nHan                    // The open file handle
   VAR lEOF                    // The end of file reached flag
   VAR nError                  // The current file error code
   VAR nLastOp                 // The last operation done (for error messages)
   VAR cBuffer                 // The readahead buffer
   VAR nReadSize               // How much to add to the readahead buffer on
                               // each read from the file

   METHOD New( cFile, nSize )  // Create a new class instance
   METHOD Open( nMode )        // Open the file for reading
   METHOD Close()              // Close the file when done
   METHOD ReadLine()           // Read a line from the file
   METHOD Name()               // Retunrs the file name
   METHOD IsOpen()             // Returns .T. if file is open
   METHOD MoreToRead()         // Returns .T. if more to be read
   METHOD Error()              // Returns .T. if error occurred
   METHOD ErrorNo()            // Returns current error code
   METHOD ErrorMsg( cText )    // Returns formatted error message

   PROTECTED:

   METHOD EOL_pos()

END CLASS

METHOD New( cFile, nSize ) CLASS TFileRead

   IF nSize == NIL .OR. nSize < 1
      // The readahead size can be set to as little as 1 byte, or as much as
      // 65535 bytes, but venturing out of bounds forces the default size.
      nSize := oF_DEFAULT_READ_SIZE
   ENDIF

   ::cFile     := cFile             // Save the file name
   ::nHan      := -1                // It's not open yet
   ::lEOF      := .T.               // So it must be at EOF
   ::nError    := 0                 // But there haven't been any errors
   ::nLastOp   := oF_CREATE_OBJECT  // Because we just created the class
   ::cBuffer   := ""                // and nothing has been read yet
   ::nReadSize := nSize             // But will be in this size chunks

   RETURN Self

METHOD Open( nMode ) CLASS TFileRead

   IF ::nHan == -1
      // Only open the file if it isn't already open.
      IF nMode == NIL
         nMode := FO_READ + FO_SHARED   // Default to shared read-only mode
      ENDIF
      ::nLastOp := oF_OPEN_FILE
      ::nHan := FOPEN( ::cFile, nMode )   // Try to open the file
      IF ::nHan == -1
         ::nError := FERROR()       // It didn't work
         ::lEOF   := .T.            // So force EOF
      ELSE
         ::nError := 0              // It worked
         ::lEOF   := .F.            // So clear EOF
      ENDIF
   ELSE
      // The file is already open, so rewind to the beginning.
      IF FSEEK( ::nHan, 0 ) == 0
         ::lEOF := .F.              // Definitely not at EOF
      ELSE
         ::nError := FERROR()       // Save error code if not at BOF
      ENDIF
      ::cBuffer := ""               // Clear the readahead buffer
   ENDIF

   RETURN Self

METHOD ReadLine() CLASS TFileRead
   LOCAL cLine := ""
   LOCAL nPos

   ::nLastOp := oF_READ_FILE

   IF ::nHan == -1
      ::nError := -1                // Set unknown error if file not open
   ELSE
      // Is there a whole line in the readahead buffer?
      nPos := ::EOL_pos()
      WHILE ( nPos <= 0 .OR. nPos > LEN( ::cBuffer ) - 3 ) .AND. !::lEOF
         // Either no or maybe, but there is possibly more to be read.
         // Maybe means that we found either a CR or an LF, but we don't
         // have enough characters to discriminate between the three types
         // of end of line conditions that the class recognizes (see below).
         cLine := FREADSTR( ::nHan, ::nReadSize )
         IF EMPTY( cLine )
            // There was nothing more to be read. Why? (Error or EOF.)
            ::nError := FERROR()
            IF ::nError == 0
               // Because the file is at EOF.
               ::lEOF := .T.
            ENDIF
         ELSE
            // Add what was read to the readahead buffer.
            ::cBuffer += cLine
         ENDIF
         // Is there a whole line in the readahead buffer yet?
         nPos := ::EOL_pos()
      END WHILE
      // Is there a whole line in the readahead buffer?
      IF nPos <= 0
         // No, which means that there is nothing left in the file either, so
         // return the entire buffer contents as the last line in the file.
         cLine := ::cBuffer
         ::cBuffer := ""
      ELSE
         // Yes. Is there anything in the line?
         IF nPos > 1
            // Yes, so return the contents.
            cLine := LEFT( ::cBuffer, nPos - 1 )
         ELSE
            // No, so return an empty string.
            cLine := ""
         ENDIF
         // Deal with multiple possible end of line conditions.
         DO CASE
            CASE SUBSTR( ::cBuffer, nPos, 3 ) == CHR( 13 ) + CHR( 13 ) + CHR( 10 )
               // It's a messed up DOS newline (such as that created by a program
               // that uses "\r\n" as newline when writing to a text mode file,
               // which causes the '\n' to expand to "\r\n", giving "\r\r\n").
               nPos += 3
            CASE SUBSTR( ::cBuffer, nPos, 2 ) == CHR( 13 ) + CHR( 10 )
               // It's a standard DOS newline
               nPos += 2
            OTHERWISE
               // It's probably a Mac or Unix newline
               nPos++
         ENDCASE
         ::cBuffer := SUBSTR( ::cBuffer, nPos )
      ENDIF
   ENDIF

   RETURN cLine

METHOD EOL_pos() CLASS TFileRead
   LOCAL nCRpos, nLFpos, nPos

   // Look for both CR and LF in the file read buffer.
   nCRpos := AT( CHR( 13 ), ::cBuffer )
   nLFpos := AT( CHR( 10 ), ::cBuffer )
   DO CASE
      CASE nCRpos == 0
         // If there's no CR, use the LF position.
         nPos := _ nLFpos == 0
         // If there's no LF, use the CR position.
         nPos := nCRpos
      OTHERWISE
         // If there's both a CR and an LF, use the position of the first one.
         nPos := MIN( nCRpos, nLFpos )
   ENDCASE

   RETURN nPos

METHOD Close() CLASS TFileRead

   ::nLastOp := oF_CLOSE_FILE
   ::lEOF := .T.
   // Is the file already closed.
   IF ::nHan == -1
      // Yes, so indicate an unknown error.
      ::nError := -1
   ELSE
      // No, so close it already!
      FCLOSE( ::nHan )
      ::nError := FERROR()
      ::nHan   := -1                // The file is no longer open
      ::lEOF   := .T.               // So force an EOF condition
   ENDIF

   RETURN Self

METHOD Name() CLASS TFileRead
   // Returns the filename associated with this class instance.
   RETURN ::cFile

METHOD IsOpen() CLASS TFileRead
   // Returns .T. if the file is open.
   RETURN ::nHan != -1

METHOD MoreToRead() CLASS TFileRead
   // Returns .T. if there is more to be read from either the file or the
   // readahead buffer. Only when both are exhausted is there no more to read.
   RETURN !::lEOF .OR. !EMPTY( ::cBuffer )

METHOD Error() CLASS TFileRead
   // Returns .T. if an error was recorded.
   RETURN ::nError != 0

METHOD ErrorNo() CLASS TFileRead
   // Returns the last error code that was recorded.
   RETURN ::nError

METHOD ErrorMsg( cText ) CLASS TFileRead
   STATIC s_cAction := {"on", "creating object for", "opening", "reading from", "closing"}

   LOCAL cMessage, nTemp

   // Has an error been recorded?
   IF ::nError == 0
      // No, so report that.
      cMessage := "No errors have been recorded for " + ::cFile
   ELSE
      // Yes, so format a nice error message, while avoiding a bounds error.
      IF ::nLastOp < oF_ERROR_MIN .OR. ::nLastOp > oF_ERROR_MAX
         nTemp := 1
      ELSE
         nTemp := ::nLastOp + 1
      ENDIF
      cMessage := iif( EMPTY( cText ), "", cText ) + "Error " + hb_ntos( ::nError ) + " " + s_cAction[ nTemp ] + " " + ::cFile
   ENDIF

   RETURN cMessage
 
 
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

FUNCTION Hb_NtoS( n ) ; RETURN ltrim( str( n ) )
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

I tried that module with the below data file :

Code: Select all


**                                   BANK OF Cyprus
A                                         ???????
    ??? ????                 RETAIL????? ?? ????? ????                         12-07-2012          ???????
    ???? ???                 103000963002        P    SEND BY POST                   1             ???? ???
    ???
        ???? ???? ??????? ????
        ?9 - ?????  ? - ????? ????????
        ?????? ??????? ??? ???

 

that below code does not generate test.DBF

Code: Select all

PROCEDURE FillDB
LOCAL oFile := TFileRead():New( "all.txt101_data_P2.txt" )
LOCAL cTemp := ""
LOCAL cAcNb := ""

CreateDBF()

oFile:Open()
IF oFile:Error()
    msgStop( oFile:ErrorMsg( "FileRead: " ) )
ELSE
    WHILE oFile:MoreToRead()
    cTemp := oFile:ReadLine()
            IF "BANK OF Cyprus" $ cTemp
            ? "hello alex"
        cTemp := oFile:ReadLine()
        cTemp := oFile:ReadLine()
        cTemp := oFile:ReadLine()
        
            IF cAcNb != padL( allTrim( subStr( cTemp, 30, 12) )) // New Account
                cAcNb := padL( allTrim( subStr( cTemp, 30 , 12) ))
                test->( dbAppend() )
                test->ACNB := cAcNb
            ENDIF
        ELSE
            //IF !"Total:" $ cTemp .AND. !"EOF:" $ cTemp
            //    test->TEXT += ( " " + allTrim( cTemp ) )
            //ENDIF
        ENDIF
    ENDDO
    oFile:Close()
ENDIF
test->( dbCloseArea() )
RETURN

STATIC PROCEDURE CreateDBF()
LOCAL aStru := {}

aAdd( aStru, { "ACNB", "C",  12, 0 } )
//aAdd( aStru, { "TEXT", "M", 10, 0 } )
dbCreate( "TEST", aStru,, .T., "test" )
RETURN

 
Ehab Samir Aziz
Posts: 334
Joined: Fri Oct 14, 2005 1:54 pm

Re: creating DBF file from text file

Post by Ehab Samir Aziz »

That module works with the sample text file but that was not my desire . It saves all the pages concerning the account number 444 into one memo field . That was not my desire . My desire is to save every page into a separate memo field . So I will have 7 records with the account 444 and 3 records with the account 445.
Thanks
ADutheil
Posts: 352
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: creating DBF file from text file

Post by ADutheil »

Changing your loop should do the trick:

Code: Select all

    WHILE oFile:MoreToRead()
        cTemp := oFile:ReadLine()
        IF "Account number" $ cTemp
            test->( dbAppend() )
            test->ACNB := padL( allTrim( subStr( cTemp, rAt( ":", cTemp ) ) ), 8 )
        ELSE
            IF !"Total:" $ cTemp .AND. !"EOF:" $ cTemp
                test->TEXT += ( " " + allTrim( cTemp ) )
            ENDIF
        ENDIF
    ENDDO
 
I´m going to travel to a remote location now and I´ll probably be off-line until tuesday.
Regards,

André Dutheil
FWH 13.04 HB 3.2 BCC 5.82 MinGW 4.5.2 MSVS 10
Post Reply