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()

C5_MEMOWRIT

 MEMOWRIT()
 Write a character string or memo field to a disk file
------------------------------------------------------------------------------
 Syntax

     MEMOWRIT(<cFile>, <cString>) --> lSuccess

 Arguments

     <cFile> is the name of the target disk file including the file
     extension and optional path and drive designator.

     <cString> is the character string or memo field to write to <cFile>.

 Returns

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

 Description

     MEMOWRIT() is a memo function that writes a character string or memo
     field to a disk file.  If a path is not specified, MEMOWRIT() writes
     <cFile> to the current DOS directory and not the current DEFAULT
     directory.  If <cFile> already exists, it is overwritten.

     MEMOWRIT() is generally used with MEMOREAD() to load text files into
     memory where they can be edited, displayed, and written back to disk.
     You can also use MEMOWRIT() as a quick way of exporting a memo field to
     a text file.

 Examples

     .  This example uses MEMOWRIT() with MEMOREAD() to allow editing
        of memo fields with an external editor:

        LOCAL cEditor := "MYEDIT.EXE"
        USE Sales NEW
        IF MEMOWRIT("Cliptmp.txt", Notes)
           RUN (cEditor + " Cliptmp.txt")
           REPLACE Notes WITH MEMOREAD("Cliptmp.txt")
        ELSE
           ? "Error while writing Cliptmp.txt"
           BREAK
        ENDIF

 Files   Library is EXTEND.LIB.

See Also: MEMOEDIT() MEMOREAD()

 

C5 Data Manipulation Functions

Array :

AADD() :

Add a new element to the end of an array

AADD( <aTarget>, <expValue> ) --> Value

ACLONE() :

Duplicate a nested or multidimensional array

ACLONE( <aSource> ) --> aDuplicate

ACOPY() :

Copy elements from one array to another

ACOPY( <aSource>, <aTarget>, [ <nStart> ], [ <nCount> ], 
    [ <nTargetPos> ] ) --> aTarget

ADEL() :

Delete an array element

ADEL( <aTarget>, <nPosition> ) --> aTarget

ADIR()* :

Fill a series of arrays with directory information

ADIR([ <cFilespec> ],
    [ <aFilenames> ],
    [ <aSizes> ],
    [ <aDates> ],
    [ <aTimes> ],
    [ <aAttributes> ] ) --> nFiles

AEVAL() :

Execute a code block for each element in an array

AEVAL( <aArray>, <bBlock>, [ <nStart> ], [ <nCount> ] ) --> aArray

AFILL() :

Fill an array with a specified value

AFILL( <aTarget>, <expValue>, [ <nStart> ], [ <nCount> ] )
       --> aTarget

AINS() :

Insert a NIL element into an array

AINS( <aTarget>, <nPosition> ) --> aTarget

ARRAY() :

Create an uninitialized array of specified length

ARRAY( <nElements> [, <nElements>...] ) --> aArray

ASCAN() :

Scan an array for a value or until a block returns (.T.)

ASCAN( <aTarget>, <expSearch>, [ <nStart> ], [ <nCount> ] )
      --> nStoppedAt

ASIZE() :

Grow or shrink an array

ASIZE( <aTarget>, <nLength> ) --> aTarget

ASORT() :

Sort an array

ASORT( <aTarget>, [ <nStart> ], [ <nCount> ], [ <bOrder> ] )
       --> aTarget

ATAIL() :

Return value of the highest numbered (last) element of an array

ATAIL( <aArray> ) --> Element

Common :

EMPTY() :

Determine if the result of an expression is empty

EMPTY( <exp> ) --> lEmpty

LEN() :

Return the length of a character string or array size

LEN( <cString> | <aArray> ) --> nCount

MAX() :

Return the larger of two numeric or date values

MAX( <nExp1>, <nExp2> ) --> nLarger
MAX( <dExp1>, <dExp2> ) --> dLarger

MIN() :

Return the smaller of two numeric or date values

MIN( <nExp1>, <nExp2> ) --> nSmaller
MIN( <dExp1>, <dExp2> ) --> dSmaller

PAD() :

Pad character, date or numeric values with a fill character

PADL( <exp>, <nLength>, [ <cFillChar> ] ) --> cPaddedString
PADC( <exp>, <nLength>, [ <cFillChar> ] ) --> cPaddedString
PADR( <exp>, <nLength>, [ <cFillChar> ] ) --> cPaddedString

TRANSFORM() :

Convert any value into a formatted character string

TRANSFORM( <exp>, <cSayPicture> ) --> cFormatString

TYPE() :

Determine the type of an expression

TYPE( <cExp> ) --> cType

VALTYPE() :

Determine the data type returned by an expression

VALTYPE( <exp> ) --> cType

Date & Time :

CDOW() :

Convert a date value to a character day of the week

CDOW( <dExp> ) --> cDayName

CMONTH() :

Convert a date to a character month name

CMONTH( <dDate> ) --> cMonth

CTOD() :

Convert a date string to a date value

CTOD( <cDate> ) --> dDate

DATE() :

Return the system date as a date value

DATE() --> dSystem

DAY() :

Return the day of the month as a numeric value

DAY( <dDate> ) --> nDay

DOW() :

Convert a date value to a numeric day of the week

DOW( <dDate> ) --> nDay

DTOC() :

Convert a date value to a character string

DTOC( <dDate> ) --> cDate

DTOS() :

Convert a date value to a string formatted as yyyymmdd

DTOS( <dDate> ) --> cDate

MONTH() :

Convert a date value to the number of the month

MONTH( <dDate> ) --> nMonth

SECONDS() :

Return the number of seconds elapsed since midnight

SECONDS() --> nSeconds

TIME() :

Return the system time

TIME() --> cTimeString

YEAR() :

Convert a date value to the year as a numeric value

YEAR( <dDate> ) --> nYear

Numeric :

ABS() :

Return the absolute value of a numeric expression

ABS( <nExp> ) --> nPositive

BIN2I() :

Convert a 16-bit signed integer to a numeric value

BIN2I( <cSignedInt> ) --> nNumber

BIN2L() :

Convert a 32-bit signed integer to a numeric value

BIN2L( <cSignedInt> ) --> nNumber

BIN2W() :

Convert a 16-bit unsigned integer to a numeric value

BIN2W( <cUnsignedInt> ) --> nNumber

EXP() :

Calculate e**x

EXP( <nExponent> ) --> nAntilogarithm

INT() :

Convert a numeric value to an integer

INT( <nExp> ) --> nInteger

I2BIN() :

Convert a numeric to a 16-bit binary integer

I2BIN( <nInteger> ) --> cBinaryInteger

LOG() :

Calculate the natural logarithm of a numeric value

LOG( <nExp> ) --> nNaturalLog

L2BIN() :

Convert a numeric value to a 32-bit binary integer

L2BIN( <nExp> ) --> cBinaryInteger

MOD()* :

Return dBASE III PLUS modulus of two numbers

MOD( <nDividend>, <nDivisor> ) --> nRemainder

ROUND() :

Return a value rounded to a specified number of digits

ROUND( <nNumber>, <nDecimals> ) --> nRounded

SQRT() :

Return the square root of a positive number

SQRT( <nNumber> ) --> nRoot

VAL() :

Convert a character number to numeric type

VAL( <cNumber> ) --> nNumber

String & Memo :

ALLTRIM() :

Remove leading and trailing spaces from character string

ALLTRIM( <cString> ) --> cTrimString

ASC() :

Convert a character to its ASCII value

ASC( <cExp> ) --> nCode

AT() :

Return the position of a substring within a string

AT( <cSearch>, <cTarget> ) --> nPosition

CHR() :

Convert an ASCII code to a character value

CHR( <nCode> ) --> cChar

HARDCR() :

Replace all soft CRs with hard CRs

HARDCR( <cString> ) --> cConvertedString

ISALPHA() :

Determine if the leftmost character is alphabetic

ISALPHA( <cString> ) --> lBoolean

ISDIGIT() :

Determine if the leftmost character is a digit

ISDIGIT( <cString> ) --> lBoolean

ISLOWER() :

Determine if the leftmost character is a lower case letter

ISLOWER( <cString> ) --> lBoolean

ISUPPER() :

Determine if the leftmost character is upper case

ISUPPER( <cString> ) --> lBoolean

LEFT() :

Extract a substring beginning with the first character

LEFT( <cString>, <nCount> ) --> cSubString

LOWER() :

Convert uppercase characters to lowercase

LOWER( <cString> ) --> cLowerString

LTRIM() :

Remove leading spaces from a character string

LTRIM( <cString> ) --> cTrimString

MEMOEDIT() :

Display or edit character strings and memo fields

MEMOEDIT( [ <cString> ],
    [ <nTop> ], [ <nLeft> ],
    [ <nBottom> ], [ <nRight> ],
    [ <lEditMode> ],
    [ <cUserFunction> ],
    [ <nLineLength> ],
    [ <nTabSize> ],
    [ <nTextBufferRow> ],
    [ <nTextBufferColumn> ],
    [ <nWindowRow> ],
    [ <nWindowColumn> ] ) --> cTextBuffer

MEMOLINE() :

Extract a line of text from character string or memo field

MEMOLINE( <cString>,
    [ <nLineLength> ],
    [ <nLineNumber> ],
    [ <nTabSize> ],
    [ <lWrap> ] ) --> cLine

MEMOREAD() :

Return the contents of a disk file as a character string

MEMOREAD( <cFile> ) --> cString

MEMOTRAN() :

Replace carriage return/line feeds in character strings

MEMOTRAN( <cString>,
    [ <cReplaceHardCR> ],
    [ <cReplaceSoftCR> ] ) --> cNewString

MEMOWRIT() :

Write a character string or memo field to a disk file

MEMOWRIT( <cFile>, <cString> ) --> lSuccess

MLCOUNT() :

Count the lines in a character string or memo field

MLCOUNT( <cString>, [ <nLineLength> ],  [ <nTabSize> ],
         [ <lWrap> ] ) --> nLines

MLCTOPOS() :

Return byte position based on line and column position

MLCTOPOS( <cText>, <nWidth>, <nLine>,
    <nCol>, [ <nTabSize> ], [ <lWrap> ] ) --> nPosition

MLPOS() :

Determine the position of a line in a memo field

MLPOS( <cString>, <nLineLength>,
     <nLine>, [ <nTabSize> ], [ <lWrap> ] ) --> nPosition

MPOSTOLC() :

Return line and column position based on byte position

MPOSTOLC( <cText>, <nWidth>, <nPos>,
     [ <nTabSize> ], [ <lWrap> ] ) --> aLineColumn

RAT() :

Return the position of the last occurrence of a substring

RAT( <cSearch>, <cTarget> ) --> nPosition

REPLICATE() :

Return a string repeated a specified number of times

REPLICATE( <cString>, <nCount> ) --> cRepeatedString

RIGHT() :

Return a substring beginning with rightmost character

RIGHT( <cString>, <nCount> ) --> cSubString

RTRIM() :

Remove trailing spaces from a character string

RTRIM( <cString> ) --> cTrimString

SET EXACT* :

Toggle exact matches for character strings

SET EXACT on | OFF | <xlToggle>

SOUNDEX() :

Convert a character string to soundex form

SOUNDEX( <cString> ) --> cSoundexString

SPACE() :

Return a string of spaces

SPACE( <nCount> ) --> cSpaces

STR() :

Convert a numeric expression to a character string

STR( <nNumber>, [ <nLength> ], [ <nDecimals> ] ) --> cNumber

STRTRAN() :

Search and replace characters within a character string

STRTRAN( <cString>, <cSearch>, [ <cReplace> ],
    [ <nStart> ], [ <nCount> ] ) --> cNewString

STUFF() :

Delete and insert characters in a string

STUFF( <cString>, <nStart>, <nDelete>, <cInsert> ) --> cNewString

SUBSTR() :

Extract a substring from a character string

SUBSTR( <cString>, <nStart>, [ <nCount> ] ) --> cSubstring

TRIM() :

Remove trailing spaces from a character string

TRIM( <cString> ) --> cTrimString

UPPER() :

Convert lowercase characters to uppercase

UPPER( <cString> ) --> cUpperString