The FILEGET function returns text from a non-binary file that has been opened for reading. When FILEGET reaches the end of the file, it returns NA
. All text read with FILEGET is translated into the database character set. FILEGET cannot read data that cannot be represented in the database character set.
An INTEGER value that was assigned to a file opened for reading in a previous call to the FILEOPEN function.
An INTEGER expression specifying the number of bytes FILEGET returns from the file. When an end-of-line character is reached in the input file, FILEGET simply starts a new line in the result it is constructing. When LENGTH is omitted, FILEGET reads one line or record regardless of how many bytes it contains.
Difference Between Number of Bytes Read and Number of Bytes Returned
The value specified by LENGTH refers to the number of bytes that the FILEGET function returns, not to the number of bytes that it reads. In some cases, these values may differ. For example, when the file being read contains a tab character, the number of bytes returned by FILEGET includes the bytes for tab expansion (if any); consequently, the number of bytes returned by FILEGET could be larger than the number of bytes read by FILEGET.
Example 7-89 Program for Reading a File
Suppose you have a program called readfile
that takes a file name as its argument. It opens the file, reads the lines of the file, adds them to a multiline text variable named wholetext
, then closes it. readfile
uses local variables to store the fileunit number and each line of the file as it is read.
DEFINE wholetext VARIABLE TEXT LD Multiline text variable DEFINE readfile PROGRAM LD Program to store data from a file in a multiline text variable PROGRAM VARIABLE fil.unit INTEGER "Local Var To Store File Unit VARIABLE fil.text TEXT "Local Var To Store Single Lines FIL.UNIT = FILEOPEN(ARG(1) READ) FIL.TEXT = FILEGET(fil.unit) "Read The First Line WHILE fil.text NE NA "Test For End-of-file DO wholetext = JOINLINES(wholetext, fil.text) fil.text = FILEGET(fil.unit) "Read The Next Line DOEND FILECLOSE fil.unit END