FileRead

Description

Reads data from the file associated with the specified file number, which was assigned to the file with the FileOpen function. FileRead is maintained for backward compatibility. Use the FileReadEx function for new development.

NoteFile format FileRead can read Unicode files in line mode, and ANSI and Unicode files in stream mode. To read from an ANSI file in stream mode, use the FromANSI function to convert an ANSI blob into a Unicode character string.

Syntax

FileRead ( file#, variable )

Argument

Description

file#

The integer assigned to the file when it was opened

variable

The name of the string or blob variable into which you want to read the data

Returns

Integer. Returns the number of characters or bytes read. If an end-of-file mark (EOF) is encountered before any characters are read, FileRead returns -100. If the file is opened in LineMode and a CR or LF is encountered before any characters are read, FileRead returns 0. If an error occurs, FileRead returns -1. If any argument’s value is null, FileRead returns null. If the file length is greater than 32,765 bytes, FileRead returns 32,765.

Usage

If the file is opened in Line mode, FileRead reads a line of the file (that is, until it encounters a CR, LF, or EOF). It stores the contents of the line in the specified variable, skips the line-end characters, and positions the file pointer at the beginning of the next line.

If the file was opened in Stream mode, FileRead reads to the end of the file or the next 32,765 bytes, whichever is shorter. FileRead begins reading at the file pointer, which is positioned at the beginning of the file when the file is opened for reading. If the file is longer than 32,765 bytes, FileRead automatically positions the pointer after each read operation so that it is ready to read the next chunk of data.

FileRead can read a maximum of 32,765 characters at a time. Therefore, before calling the FileRead function, call the FileLength function to check the file length. If your system has file sharing or security restrictions, you may need to call FileLength before you call FileOpen.

An end-of-file mark is a null character (ASCII value 0). Therefore, if the file being read contains null characters, FileRead stops reading at the first null character, interpreting it as the end of the file.

Examples

Example 1

This example reads the file EMP_DATA.TXT if it is short enough to be read with one call to FileRead:

integer li_FileNum

string ls_Emp_Input

long ll_FLength


ll_FLength = FileLength("C:\HR\EMP_DATA.TXT")

li_FileNum = FileOpen("C:\HR\EMP_DATA.TXT", &

		StreamMode!)

IF ll_FLength < 32767 THEN

		FileRead(li_FileNum, ls_Emp_Input)

END IF

Example 2

This example reads the file EMP_PIC1.BMP and stores the data in the blob Emp_Id_Pic. The number of bytes read is stored in li_bytes:

integer li_fnum, li_bytes

blob Emp_Id_Pic


li_fnum = FileOpen("C:\HR\EMP_PIC1.BMP", &

		StreamMode!)

li_bytes = FileRead(li_fnum, Emp_Id_Pic)

Example 3

This example reads a file exceeding 32,765 bytes. After the script has read the file into the blob tot_b, you can call the SetPicture or String function to make use of the data, depending on the contents of the file:

integer li_FileNum, loops, i

long flen, bytes_read, new_pos

blob b, tot_b


// Set a wait cursor

SetPointer(HourGlass!)


// Get the file length, and open the file

flen = FileLength(sle_filename.Text)

li_FileNum = FileOpen(sle_filename.Text, &

		StreamMode!, Read!, LockRead!)


// Determine how many times to call FileRead

IF flen > 32765 THEN

		IF Mod(flen, 32765) = 0 THEN

			loops = flen/32765

		ELSE

			loops = (flen/32765) + 1

		END IF

ELSE

		loops = 1

END IF


// Read the file

new_pos = 1

FOR i = 1 to loops

		bytes_read = FileRead(li_FileNum, b)

		tot_b = tot_b + b

NEXT


FileClose(li_FileNum)

See also