Function Reference

FileRead

Read in a number of characters from a previously opened text file.

FileRead ( filehandle or "filename" [, count] )

 

Parameters

filehandle The handle of a file, as returned by a previous call to FileOpen. Alternatively you may use a string filename as the first parameter.
count [optional] The number of characters to read. Default read the entire file. Not optional for file in raw reading mode

 

Return Value

Success: Returns the binary/string read.
Special: Sets @error to -1 if end-of-file is reached.
Failure: Sets @error to 1 if file not opened in read mode or other error.
Sets @error to 2 if count not defined for file open in raw read mode.

 

Remarks

If a filename is given rather than a file handle - the file will be opened and closed during the function call - for parsing large text files this will be much slower than using filehandles.
Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Use either filehandles or filenames in your routines, not both!

If the file was opened in Raw mode=4, the count must be a multiple of the sector size (512).

Both ANSI and UTF16/UTF8 text formats can be read - AutoIt will automatically determine the type.

A file can be read as binary(byte) data by using FileOpen with the binary flag - in this case count is in bytes rather than characters.

 

Related

IniRead, FileClose, FileOpen, FileReadLine, FileWrite, FileWriteLine, String

 

Example


$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in 1 character at a time until the EOF is reached
While 1
    $chars = FileRead($file, 1)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Char read:", $chars)
Wend

FileClose($file)