Previous Topic: OpenPipe - Open a PipeNext Topic: SeekFile - Reposition the Current Position in an Open File


ReadFile - Read Data from a File

Valid on NetWare, Symbian OS, UNIX, Windows and Windows CE

The ReadFile function reads data from a file.

This file content function has one of the following formats:

ReadFile (handle as Integer, buffer as String) as Boolean
ReadFile (handle as Integer, buffer as String, buflen as Integer) as Boolean
ReadFile (handle as Integer, buffer as Void) as Boolean
ReadFile (handle as Integer, buffer as Void, buflen as Integer) as Boolean
handle

Identifies a file handle returned by a previous call of OpenFile or CreateFile.

buffer

Specifies a variable of any type. It stands for the buffer to receive data from the file.

Buffers of type arrays of char are treated as strings.

If the argument buffer is a string type variable, the function reads from the current file position and stops reading when it reads a newline character or the number of characters has been reached. If the argument buffer is any other type, the function reads from the current file position and stops when the buffer is full or the file pointer reaches end of file.

Void means that the type of object can be any valid type except for string and arrays of char.

buflen

Specifies the number of bytes to be read from the file into the buffer. If there are not enough bytes left in the file, all the bytes remaining are transmitted into the buffer.

On successful completion, the function returns TRUE; otherwise, it returns FALSE.

Note: Do not mix the different formats of the ReadFile() function as this can lead to unexpected results.

Unlike the other formats, the first format reads the string as a text line that is terminated by a newline sign or end of file and stores the information to the buffer without the newline sign. The interpreter maps this function to a different system read function than the other three formats. The other ReadFile formats are mapped to a common system read function.

The different systems read functions may use different caches and read pointers to retrieve the data. If you mix the formats, the data may not be read in the sequence in which it was stored on the disk.

In the case of the first format, the string read is a text line, terminated by a newline sign or end of file. The information is stored to the buffer without this newline. This is not true for the other ReadFile functions.

Furthermore, the interpreter maps this function to a different system read function than the other three formats, which are mapped to a common system read function. These different system read functions may also use different caches and read pointers to retrieve the data. If the formats are mixed, the data may not be read in the sequence in it was stored on the disk.

Example: ReadFile function

Dim fIn, fOut as integer   ' Declare file handles
Dim OneLine as string     ' String to hold one line

Rem First open the Input file...

fIn=OpenFile("C:\CONFIG.SYS",O_READ)
if fIn<0 then
	MessageBox("Unable to open input file","Error")
	Goto End
End if

Rem ...Then create the output file...

fOut=CreateFile("C:\CONFIG.BAK")
if fOut<0 then
	MessageBox("Unable to create output file","Error")

CloseFile(fIn)
	Goto End
End if

Rem ...Copy lines until none left...

while Not(Eof(fIn))
	if ReadFile(fIn,OneLine) then WriteFile(fOut,OneLine)
wend

Rem ...Close Files, and signal success.

CloseFile(fIn)
CloseFile(fOut)
MsgBox("A backup of the CONFIG.SYS file was created","MyScript",MB_OK)

end: