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

MemoWrit()

MEMOWRIT()

Write a memo field or character string to a text file on disk

Syntax

      MEMOWRIT( <cFileName>, <cString>, [<lWriteEof>] ) --> lSuccess

Arguments

<cFileName> is the filename to read from disk. It must include the file extension. If file to be read lives in another directory, you must include the path.

<cString> Is the memo field or character string, to be write to <cFile>.

<lWriteEof> Is a logic variable that settle if the “end of file” character – CHR(26) – is written to disk. This parameter is optional. By default is true (.T.)

Returns

Function returns true (.T.) if the writing operation was successful; otherwise, it returns false (.F.).

Description

This a function that writes a memo field or character string to a text file on disk (floppy, HD, CD-ROM, etc.) If you not specified a path, MEMOWRIT() writes <cFileName> to the current directory. If <cFileName> exists, it is overwritten.

There is a third parameter (optional), <lWriteEof>, (not found in CA-Cl*pper) which let to programmer change the default behavior of – always – to write the EOF character, CHR(26) as in CA-Cl*pper.

If there is no third parameter, nothing change, EOF is written as in CA-Cl*pper, the same occurs when <lWriteEof> is set to .T. But, if <lWriteEof> is set to .F., EOF char is Not written to the end of the file.

MEMOWRIT() function is used together with MEMOREAD() and MEMOEDIT() to save to disk text from several sources that was edited, searched, replaced, displayed, etc.

Note that MEMOWRIT() do not use the directory settings SET DEFAULT.

Examples

      *  This example uses MEMOWRIT() to write the contents of a character
         variable to a text file.

         cFile   := "account.prg"
         cString := MEMOREAD( cFile )

         IF At( "Melina", cString ) == 0            // check for copyright
            MEMOWRIT( cFile, cCopyright + cString ) // if not, add it!
         ENDIF

Compliance

Clipper

Platforms

All

Files

Library is rtl

Seealso

MEMOEDIT(), MEMOREAD(), HB_MEMOWRIT()

MemoRead()

MEMOREAD()

Return the text file’s contents as a character string

Syntax

      MEMOREAD( <cFileName> ) --> cString

Arguments

<cFileName> is the filename to read from disk. It must include the file extension. If file to be read lives in another directory, you must include the path.

Returns

Returns the contents of a text file as a character string.

If <cFileName> cannot be found or read MEMOREAD returns an empty string (“”).

Description

MEMOREAD() is a function that reads the content of a text file (till now) from disk (floppy, HD, CD-ROM, etc.) into a memory string. In that way you can manipulate as any character string or assigned to a memo field to be saved in a database.

MEMOREAD() function is used together with MEMOEDIT() and MEMOWRIT() to get from disk text from several sources that would be edited, searched, replaced, displayed, etc.

It is used to import data from other sources to our database.

Note: MEMOREAD() does not use the settings SET DEFAULT or SET PATH to search for <cFileName>. It searches for <cFileName> in the current directory. If the file is not found, then MEMOREAD() searches in the DOS path.

Over a network, MEMOREAD() attempts to open <cFileName> in read-only mode and shared. If the file is used in mode exclusive by another process, the function will returns a null string (“”).

Examples

      *  This example uses MEMOREAD() to assign the contents of a text
      *  file to a character variable for later search

         cFile   := "account.prg"
         cString := MEMOREAD( cFile )
         cCopyright := "Melina"

         IF At( "Melina", cString ) == 0            // check for copyright
            MEMOWRIT( cFile, cCopyright + cString ) // if not, add it!
         ENDIF

Compliance

Clipper

Platforms

All (64K)

Files

Library is rtl

Seealso

MEMOEDIT(), MEMOWRIT(), REPLACE, HB_MEMOREAD()

hb_MemoWrit()

HB_MEMOWRIT()

Write a memo field or character string to a text file on disk

Syntax

      HB_MEMOWRIT( <cFileName>, <cString>, [<lWriteEof>] ) --> lSuccess

Arguments

<cFileName> is the filename to read from disk. It must include the file extension. If file to be read lives in another directory, you must include the path.

<cString> Is the memo field or character string, to be write to <cFile>.

<lWriteEof> Is a logic variable that settle if the “end of file” character – CHR(26) – is written to disk. This parameter is optional. By default is true (.T.)

Returns

Function returns true (.T.) if the writing operation was successful; otherwise, it returns false (.F.).

Description

This a function that writes a memo field or character string to a text file on disk (floppy, HD, CD-ROM, etc.) If you not specified a path, HB_MEMOWRIT() writes <cFileName> to the current directory. If <cFileName> exists, it is overwritten.

There is a third parameter (optional), <lWriteEof>, (not found in CA-Cl*pper) which let to programmer change the default behavior of – always – to write the EOF character, CHR(26) as in CA-Cl*pper.

If there is no third parameter, nothing change, EOF is written as in CA-Cl*pper, the same occurs when <lWriteEof> is set to .T. But, if <lWriteEof> is set to .F., EOF char is Not written to the end of the file.

HB_MEMOWRIT() function is used together with HB_MEMOREAD() and MEMOEDIT() to save to disk text from several sources that was edited, searched, replaced, displayed, etc.

Note that HB_MEMOWRIT() do not use the directory settings SET DEFAULT.

HB_MEMOWRIT() vs MEMOWRIT(): HB_MEMOWRIT() never writes the obsolete EOF char at the end of the file.

Examples

      *  This example uses HB_MEMOWRIT() to write the contents of a character
      *  variable to a text file.

         cFile   := "account.prg"
         cString := HB_MEMOREAD( cFile )
         cCopyright := "Melina"

         IF At( "Melina", cString ) == 0               // check for copyright
            HB_MEMOWRIT( cFile, cCopyright + cString ) // if not, add it!
         ENDIF

Compliance

Clipper

Platforms

All

Files

Library is rtl

Seealso

MEMOEDIT(), MEMOREAD(), HB_MEMOWRIT()

hb_MemoRead()

HB_MEMOREAD()

Return the text file’s contents as a character string

Syntax

      HB_MEMOREAD( <cFileName> ) --> cString

Arguments

<cFileName> is the filename to read from disk. It must include the file extension. If file to be read lives in another directory, you must include the path.

Returns

Returns the contents of a text file as a character string.

If <cFileName> cannot be found or read HB_MEMOREAD returns an empty string (“”).

Description

HB_MEMOREAD() is a function that reads the content of a text file (till now) from disk (floppy, HD, CD-ROM, etc.) into a memory string. In that way you can manipulate as any character string or assigned to a memo field to be saved in a database.

HB_MEMOREAD() function is used together with MEMOEDIT() and HB_MEMOWRIT() to get from disk text from several sources that would be edited, searched, replaced, displayed, etc.

It is used to import data from other sources to our database.

Note: HB_MEMOREAD() does not use the settings SET DEFAULT or SET PATH to search for <cFileName>. It searches for <cFileName> in the current directory. If the file is not found, then HB_MEMOREAD() searches in the DOS path.

Over a network, HB_MEMOREAD() attempts to open <cFileName> in read-only mode and shared. If the file is used in mode exclusive by another process, the function will returns a null string (“”).

HB_MEMOREAD() vs MEMOREAD(): HB_MEMOREAD() is identical to MEMOREAD() except it won’t truncate the last byte (on non-UNIX compatible systems) if it’s a EOF char.

Examples

      *  This example uses HB_MEMOREAD() to assign the contents of a text
      *  file to a character variable for later search

         cFile   := "account.prg"
         cString := HB_MEMOREAD( cFile )
         cCopyright := "Melina"

         IF At( "Melina", cString ) == 0               // check for copyright
            HB_MEMOWRIT( cFile, cCopyright + cString ) // if not, add it!
         ENDIF

Compliance

Clipper

Platforms

All (64K)

Files

Library is rtl

Seealso

MEMOEDIT(), HB_MEMOWRIT(), REPLACE, MEMOREAD()

C5_MEMOREAD

 MEMOREAD()
 Return the contents of a disk file as a character string
------------------------------------------------------------------------------
 Syntax

     MEMOREAD(<cFile>) --> cString

 Arguments

     <cFile> is the name of the file to read from disk.  It must include
     an extension, if there is one, and can optionally include a path.

 Returns

     MEMOREAD() returns the contents of a text file as a character string.
     The maximum file size that can be read is 65,535 characters (64K)--the
     maximum size of a character string.  If <cFile> cannot be found,
     MEMOREAD() returns a null string ("").

 Description

     MEMOREAD() is a memo function that reads a disk file into memory where
     it can be manipulated as a character string or assigned to a memo field.
     MEMOREAD() is used with MEMOEDIT() and MEMOWRIT() to edit an imported
     disk file, and then write it back to disk.  MEMOREAD() searches for
     <cFile> beginning with the current DOS directory.  If the file is not
     found, MEMOREAD() searches the DOS path.  MEMOREAD() does not use the
     Clipper DEFAULT or PATH to search for <cFile>.

     In a network environment, MEMOREAD() attempts to open the specified file
     shared and read--only.  If the file is opened exclusive by another
     process, MEMOREAD() returns a null string ("").

 Examples

     .  This example uses MEMOREAD() to assign the contents of a text
        file to the Notes memo field and to a character variable:

        REPLACE Notes WITH MEMOREAD("Temp.txt")
        cString = MEMOREAD("Temp.txt")

     .  This example defines a function that edits a disk file:

        FUNCTION Editor( cFile )
           LOCAL cString
           IF (cString := MEMOREAD(cFile)) == ""
              ? "Error reading " + cFile
              RETURN .F.
           ELSE
              MEMOWRIT(cFile, MEMOEDIT(cString))
              RETURN .T.
           ENDIF

 Files   Library is EXTEND.LIB.

See Also: MEMOEDIT() MEMOWRIT() REPLACE

 

Parsing Text – Tokens

/*
From Harbour changelog (at 2007-04-04 10:35 UTC+0200 By Przemyslaw Czerpak )
Added set of functions to manipulate string tokens:
HB_TOKENCOUNT( <cString>, [ <cDelim> ], [ <lSkipStrings> ],
 [ <lDoubleQuoteOnly> ] ) -> <nTokens>

 HB_TOKENGET( <cString>, <nToken>, [ <cDelim> ], [ <lSkipStrings> ],
 [ <lDoubleQuoteOnly> ] ) -> <cToken>

 HB_TOKENPTR( <cString>, @<nSkip>, [ <cDelim> ], [ <lSkipStrings> ],
 [ <lDoubleQuoteOnly> ] ) -> <cToken>

 HB_ATOKENS( <cString>, [ <cDelim> ], [ <lSkipStrings> ],
 [ <lDoubleQuoteOnly> ] ) -> <aTokens>

 All these functions use the same method of tokenization. They can
 accept as delimiters string longer then one character. By default
 they are using " " as delimiter. " " delimiter has special mening

 Unlike other delimiters repeted ' ' characters does not create empty
 tokens, f.e.: 

 HB_ATOKENS( " 1 2 3 " ) returns array:
 { "1", "2", "3" }

 Any other delimiters are restrictly counted, f.e.:

 HB_ATOKENS( ",,1,,2,") returns array:
 { "", "", "1", "", "2", "" }
And a strong suggession made at 2009-12-09 21:25 UTC+0100 ( By Przemyslaw Czerpak )
I strongly suggest to use hb_aTokens() and hb_token*() functions.
 They have more options and for really large data many times
 (even hundreds times) faster.

*/
#define CRLF HB_OsNewLine()
PROCEDURE Main()
LOCAL cTextFName := "Shakespeare.txt",;
 c1Line 

 SET COLO TO "W/B"
 SetMode( 40, 120 )

 CLS

 HB_MEMOWRIT( cTextFName,;
 "When in eternal lines to time thou grow'st," + CRLF + ;
 "So long as men can breathe, or eyes can see," + CRLF + ;
 "So long lives this, and this gives life to thee." )

 aLines := HB_ATOKENS( MEMOREAD( cTextFName ), CRLF )

 ?
 ? "Text file line by line :"
 ?
 AEVAL( aLines, { | c1Line | QOUT( c1Line ) } )
 ?
 WAIT "Press a key for parsing as words"
 CLS
 ?
 ? "Text file word by word :"
 ?
 FOR EACH c1Line IN aLines
 a1Line := HB_ATOKENS( c1Line ) 
 AEVAL( a1Line, { | c1Word | QOUT( c1Word ) } )
 NEXT 
 ?
 WAIT "Press a key for parsing directly as words"
 CLS
 ?
 ? "Text file directly word by word :"
 ?
 aWords := HB_ATOKENS( MEMOREAD( cTextFName ) )
 AEVAL( aWords, { | c1Word | QOUT( c1Word ) } ) 

 ?
 @ MAXROW(), 0
 WAIT "EOF TP_Token.prg" 

RETURN // TP_Token.Main()
 TP_Token

Parsing Text – FParse()

/*
FParse()
Parses a delimited text file and loads it into an array.
Syntax :
FParse( <cFileName>, <cDelimiter> ) --> aTextArray
Arguments :
<cFileName> : This is a character string holding the name of the text file to load 
 into an array. It must include path and file extension. 
 If the path is omitted from <cFileName>, 
 the file is searched in the current directory. 

 <cDelimiter> : This is a single character used to parse a single line of text. 
 It defaults to the comma.
Return :
The function returns a two dimensional array, or an empty array when the file 
cannot be opened. 

Description :

 Function FParse() reads a delimited text file and parses each line 
 of the file at <cDelimiter>. The result of line parsing is stored in an array.
This array, again, is collected in the returned array, 
 making it a two dimensional array
FParse() is mainly designed to read the comma-separated values (or CSV) file format, 
 were fields are separated with commas and records with new-line character(s). 

Library is : xHb 

*/
#define CRLF HB_OsNewLine()
PROCEDURE Main()
LOCAL cTextFName := "Shakespeare.txt",;
      a1Line 

 SET COLO TO "W/B"
 SetMode( 40, 120 )

 CLS

 HB_MEMOWRIT( cTextFName,;
              "When in eternal lines to time thou grow'st," + CRLF + ;
              "So long as men can breathe, or eyes can see," + CRLF + ;
              "So long lives this, and this gives life to thee." )

 aLines := FParse( cTextFName, " " )

 ?
 ? "Text file word by word :"
 ?
 FOR EACH a1Line IN aLines
    AEVAL( a1Line, { | c1Word | QOUT( c1Word ) } )
 NEXT 
 ?
 @ MAXROW(), 0
 WAIT "EOF TP_FParse.prg" 

RETURN // TP_FParse.Main()

TP_FParse