Text File Processing

Harbour have many features for text file processing, manipulation.

Global Processing : taking text files as a single string.

MemoRead Return the text file’s contents as a character string
MemoWrit Write a memo field or character string to a text file on disk
HB_MemoRead Return the text file’s contents as a character string
HB_MemoWrit Write a memo field or character string to a text file on disk

A Class to read a text file

TFileRead Read a file one line at a time

In Harbour it’s possible also manipulate text files same as table ( .dbf ) files.

This group of function may be used for this purpose.

The basic structure is :

HB_FUse( <cTextFile> ) <-- open text file as a table

 WHILE .NOT. HB_FEof() <-- continue process for all lines in the file
c1Line := HB_FReadLN() <-- read a record (line) from file

 .... <-- process line 

 HB_FSkip() <-- advance record ( line ) pointer

 ENDDO

 HB_FUse() <-- close file

Special Text File Processing Functions

HB_FAtEOF() Chech EOF status of given work area
HB_FEof() Check end of text file status
HB_FGoBottom() Go to the last record in a text file
HB_FGoto() Move record pointer to specific record in the selected text file
HB_FGoTop() Move the record pointer to the first record in selected text file
HB_FInfo() Retrieves status information about the currently selected text file
HB_FLastRec() Get the number of records in the currently selected text file
HB_FReadAndSkip() Reads the current line and moves the record pointer to the next line
HB_FreadLN() Read a line from the selected text file without moving the record pointer
HB_FRecno() Return the current line / record number of a text file
HB_FSelect() Select a text file work area
HB_FSkip() Move the record pointer to a new position in a text file
HB_FUse() Open or close a text file for use by the HB_F* functions

A ‘global’ sample for HB_F* text  file processing functions : download.

TFP_Test_ScreenShoots

HB_FUse

HB_FUse

Open or close a text file for use by the HB_F* functions

Syntax

      HB_FUse( [ <cFile> ] [, <nMode> ] ) -> nHandle | NIL

Arguments

<cFile> : is the text file you want to open. If not specified, the file currently open, if any, will be closed.

<nMode> : is the open mode for the file. Please refer to the discussion of open modes under FOPEN() and FILEIO.CH for a list of allowable open modes. If not specified, the file will be opened with a mode of FO_READ + FO_SHARED (64).

Returns

If <cFile> is passed and the file is opened successfully, an integer containing the file handle. If the file cannot be opened, -1 will be returned.

If HB_FUse() is called without any arguments, it will close the text file in the current “text area” and return NIL.

Description

Some of HB_F*() file functions are for reading text files, that is, files where each line (record) is delimited by a CRLF pair.

Each file is opened in its own “workarea”, similar to the concept use by dbf files. As provided, a maximum of 10 files (in 10 workareas) can be opened (assuming there are sufficient file handles available).

Example

      HB_FUse( "test.txt" )      // open text file
      DO WHILE ! HB_FEOF()
         ? HB_FReadLn()
         HB_FSkip()
      ENDDO
      HB_FUse()                // close file

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FSkip()

HB_FSkip

Move the record pointer to a new position in a text file

Syntax

      HB_FSkip( [ <nLines> ] ) -> NIL

Argument

<nLines> is the number of lines to skip. Defaults to 1 if not specified.

Returns

NIL

Description

HB_FSkip moves the text file record pointer, similar to the Harbour DBSKIP() function and SKIP command.

A text file “record” is a line of text terminated by a CRLF pair.

Example

      // display each record of a text file
      HB_FUse( "test.txt" )
      DO WHILE ! HB_FEOF()
         ? HB_FReadLn()
         HB_FSkip()
      ENDDO

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FUse()

HB_FSelect

HB_FSelect()

Select a text file workarea

Syntax

      HB_FSelect( [ <nArea> ] ) -> nArea

Argument

<nArea> is the text file workarea to select.

Returns

HB_FSelect() return the current selected text file area number

Description

HB_FSelect() selects a text file “workarea” from 1 to 10. A file may or may not be open in the selected area.

Passing 0 for <nArea> selects the next available workarea, similar to Harbour’s SELECT 0 command.

Each file is opened in its own “workarea”, similar to the concept used by dbf files. As provided, a maximum of 10 files (in 10 workareas) can be opened (assuming there are sufficient file handles available).

All the HB_F*() file functions operate on the file in the currently selected text file workarea.

Text file workareas are separate from and independent of Harbour’s database workareas.

Example

      HB_FSelect(1)
      nFile1 := HB_FUse( "test.txt" )
      ? HB_FLastRec()                 // no. of lines in test.txt
      HB_FSelect(2)
      nFile2 := HB_FUse( "temp.txt" )
      ? HB_FLastRec()                 // no. of lines in temp.txt

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSkip(), HB_FUse()

HB_FRecNo

HB_FRecNo

Return the current line / record number of a text file

Syntax

      HB_FRecNo() -> nRecNo

Argument

None

Returns

HB_FRecNo() returns the current record number of a text file or 0 if no file is open.

Description

This function returns the current record number of the file open in the currently selected text file workarea.

A text file “record” is a line of text terminated by a CRLF pair.

Example

      HB_FUse( "test.txt" )      // open text file
      DO WHILE !HB_FEOF()
         ? HB_FReadLn()          // read a line / record
         HB_FSkip()              // go to next line / record
      ENDDO
      HB_FGoTop()                // go to top of file ( first line / record )
      ? HB_FRecNo()            // 1

      *˜˜˜˜˜˜˜˜˜˜˜˜˜˜

      // The example lists the lines of a text file along with
      // their line numbers

      PROCEDURE Main
         LOCAL cFile  := "text.txt"

         nFile := HB_FUse( cFile )

         DO WHILE .NOT. HB_FEof()
            ? "Line #" + LTRIM( STR( HB_FRecno() ) ), HB_FReadLN()
            HB_FSkip(1)
         ENDDO

         HB_FUse()
      RETURN

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FSelect(), HB_FSkip(), HB_FUse()

HB_FReadLn

HB_FReadLn

Read a line from the currently selected text file without moving the record pointer

Syntax

      HB_FReadLn() -> cLine

Argument

None

Returns

A string containing the current record in a text file

Description

HB_FReadLn() reads the current line in the currently selected text file workarea.

The record pointer is not moved.

A text file “record” is a line of text terminated by a CRLF pair.

Example

      // display each record of a text file
      HB_FUse( "test.txt" )
      DO WHILE ! HB_FEOF()
         ? HB_FReadLn()
         HB_FSkip()
      ENDDO

      * ˜˜˜˜˜˜˜˜˜˜˜˜˜˜

      // The example fills an array with the lines of a text file,
      // by skipping through the entire file.

      PROCEDURE Main
         LOCAL aLines := {}
         LOCAL cFile  := "Test.txt"
         LOCAL nFile

         nFile := HB_FUse( cFile )

         DO WHILE .NOT. HB_FEof()
            AAdd( aLines, HB_FReadLN() )
            HB_FSkip()
         ENDDO

         HB_FUse()

         AEval( aLines, {|cLine| QOut( cLine ) } )

      RETURN

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FRecno(), HB_FSelect(), HB_FSkip(), HB_FUse()

HB_FReadAndSkip

HB_FReadAndSkip()

Reads the current line and moves the record pointer to the next line / record

Syntax

      HB_FReadAndSkip() --> cLine

Argument

None

Return

The current line in the currently selected text file as a character string.

Description

HB_FReadAndSkip() reads the current line in the currently selected text file and advances the record pointer to the next line.

Example

      // The example fills an array with the lines of a text file,
      // taking advantage of implicit record pointer movement of
      // function HB_FReadAndSkip().

      PROCEDURE Main
         LOCAL cFile := "Textfile.txt"
         LOCAL aLines, nLine := 0, nCount
         LOCAL nFile

         nFile  := HB_FUse( cFile )
         nCount := HB_FLastRec() + 1
         aLines := Array( nCount )

         DO WHILE ++nLine <= nCount
            aLines[ nLine ] := HB_FReadAndSkip()
         ENDDO

         HB_FUse()

         AEval( aLines, {|cLine| QOut( cLine ) } )
      RETURN

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FSkip(), HB_FUse()

HB_FLastRec

HB_FLastRec

Get the number of records in the currently selected text file

Syntax

      HB_FLastRec() -> nLastRecordNum

Argument

None

Returns

An integer containing the number of records in the text file in the currently selected text file workarea, or zero if no file is currently open in the workarea or file is empty

Description

HB_FLastRec() returns the number of the last record in a text file.

A text file “record” is a line of text terminated by a CRLF pair.

Examples

      HB_FUse( "test.txt" )
      ? HB_FLastRec()

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FInfo(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FSkip(), HB_FUse()

HB_FInfo

HB_FInfo()

Retrieves status information about the currently selected text file

Syntax

      HB_Finfo() --> aInfo

Return

HB_FInfo() returns a one dimensional array with six elements, holding information about the currently selected text file

Description

File information array :

         Element Description
         ------- ---------------------------------
             1   Currently selected text file area
             2   Line count according to HB_FLastRec()
             3   Current line according to HB_FRecno()
             4   Position of file pointer
             5   File size in bytes
             6   End-of-file flag according to HB_FEof()

Example

      HB_FUse( "test.txt" )
      aTFInfo := HB_FInfo()
      ? 'Work area #  : ',  aTFInfo[ 1 ]
      ? 'Line count   : ',  aTFInfo[ 2 ]
      ? 'Curr. Rec #  : ',  aTFInfo[ 3 ]
      ? 'File pointer : ',  aTFInfo[ 4 ]
      ? 'File size    : ',  aTFInfo[ 5 ], 'bytes'
      ? 'End of file  : ',  aTFInfo[ 6 ]

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoto(), HB_FGoTop(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FSkip(), HB_FUse()

HB_FGoTo

HB_FGoTo

Move record pointer to specific record in the selected text file

Syntax

      HB_FGoTo( nLine ) -> NIL

Argument

<nLine> : the record number to go to

Returns

NIL

Description

HB_FGoTo() moves the record pointer to a specific record in the file in the currently selected text file workarea. If the record number requested is greater than the number of records in the file, the record pointer will be positioned at the last record.

A text file “record” is a line of text terminated by a CRLF pair.

Example

      // read 5th line of text from file
      HB_FUse( ( "test.txt" )
      HB_FGoTo( (5)
      c1Line := HB_FReadLn()

Seealso

HB_FAtEOF(), HB_FEof(), HB_FGoBottom(), HB_FGoTop(), HB_FInfo(), HB_FLastRec(), HB_FReadAndSkip(), HB_FreadLN(), HB_FRecno(), HB_FSelect(), HB_FSkip(), HB_FUse()