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

Harbour All Functions – T

TabExpand
TabPack

Tan

TanH

TBrowseDB

TBrowseNew

TFileRead

THtml

Time

TimeValid

TNortonGuide 

Token
TokenAt
TokenEnd
TokenExit
TokenInit
TokenLower
TokenNext
TokenNum
TokenSep
TokenUpper

Tone

TOs2

Transform
Trim

TRtf

TTroff

 Type

SP_FILEREAD

FILEREAD()

  Short:
  ------
  FILEREAD() Lists a text file of unlimited size

  Returns:
  --------
  nothing

  Syntax:
  -------
  FILEREAD([nTop,nLeft,nBottom,nRight],[cFileName],[cTitle],;
            [lSearch],[lMark])

  Description:
  ------------
  Lists text file [cFileName] of unlimited size in a
  programmer definable window of dimensions [nTop..nRight]

  Allows up down right left scrolling. Use this for
  reports or output sent to a disk file.

  If [cFileName]  is not passed, a box asks for the
  filespec and then allows a picklist of files of that spec. If
  [cFileName] is passed as a wildcard (i.e. "*.DOC"). a picklist
  of files of that spec is presented.

  If [nTop..nRight] are not passed, a default window of
  dimensions 2,2,22,78 is used.

  [cTitle] is an optional title. This overrides the
  default which is the file name.

  [lSearch] is a switch to allow text searches. Default
  is True

  [lMark] is a switch to allow block marking (with Copy
  to File or Print) Default is True.

  Examples:
  ---------
   REPORT FORM summary TO summary.txt
   FILEREAD(2,2,22,78,"SUMMARY.TXT","Summary File")

  Notes:
  -------
  Fileread() will use SET DEFAULT if no path is
  specified.

  Source:
  -------
  S_FILER.PRG

 

TFileRead()

TFileRead()

Read a file one line at a time

Syntax

      oFile := TFileRead():New( <cFileName> [, <nReadSize> ] )

Arguments

<cFileName> is the required name of the file to be read.

<nReadSize> is the optional size to use when reading from the file. The default value is 4096 and the allowed range is 1 through 65535. Any value outside of this range causes the default value to be used.

Returns

An instance of the File Reader class

Description

TFileRead() is used to access a file one line at a time. You must specify the name of the file when an instance of the class is created.

The class data should be considered private to the class.

The class methods are as follows:

New() Creates a new instance of the TFileRead class.

Open([<nFlags>]) Opens the file for reading. The optional nFlags parameter can use any of the FOPEN() flags from fileio.ch. The default is FO_READ + FO_SHARED. Calling this method when the file is already open causes the next ReadLine() to start over from the beginning of the file.

Close() Closes the file.

ReadLine() Returns one line from the file, stripping the newline characters. The following sequences are treated as one newline:

          1) CR CR LF; 
          2) CR LF; 
          3) LF; and 
          4) CR. 

          Note: LF CR is 2 newlines.

Name() Returns the name of the file.

IsOpen() Returns .T. if the file is open.

MoreToRead() Returns .T. if there are more lines to be read (think of it as an inverse EOF function).

Error() Returns .T. if an error has occurred

ErrorNo() Returns the current error code.

ErrorMsg([<cPre>]) Returns a formatted error message.

Examples

      PROCEDURE Main( cFile )
         LOCAL oFile := TFileRead():New( cFile )

         oFile:Open()
         IF oFile:Error()
            OutStd( oFile:ErrorMsg( "FileRead: " ) )
            OutStd( hb_eol() )
         ELSE
            DO WHILE oFile:MoreToRead()
               OutStd( oFile:ReadLine() )
               OutStd( hb_eol() )
            ENDDO
            oFile:Close()
         ENDIF
         RETURN

Tests

      See Examples

Compliance

This is a new Harbour Tools class

Files

Library is libmisc

Seealso

TClass()

Download a test program (File Copy)  (Test_TFR.prg).