Function Reference


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

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


filehandle/filename 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. See remarks.

Return Value

Success: the binary/string read. @extended is set to the number of bytes/characters returned.
Failure: sets the @error flag to non-zero.
@error: 1 = if file not opened in read mode or other error
-1 = if end-of-file is reached


A negative or not defined count, reads the whole file from the current position.

If a filename is given rather than a file handle - the file will be opened and closed during the function call - for parsing large 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!

Both ANSI and UTF16/UTF8 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. A count value that is too large can lead to AutoIt stopping with a memory allocation failure.


FileGetPos(), FileOpen(), FileReadLine(), FileSetPos(), FileWrite(), FileWriteLine(), String()


#include <FileConstants.au3>
#include <MsgBoxConstants.au3>


Func Example()
    ; Create a constant variable in Local scope of the filepath that will be read/written to.
    Local Const $sFilePath = @TempDir & "\FileRead.txt"

    ; Create a temporary file to read data from.
    If Not FileCreate($sFilePath, "This is an example of using FileRead.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")

    ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False

    ; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)

    ; Close the handle returned by FileOpen.

    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)

    ; Delete the temporary file.
EndFunc   ;==>Example

; Create a file.
Func FileCreate($sFilePath, $sString)
    Local $bReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
EndFunc   ;==>FileCreate