Opens a file for reading or writing.
FileOpen ( "filename" [, mode = 0] )
The file handle must be closed with the FileClose() function.
A file may fail to open due to access rights or attributes.
The default mode when writing text is UTF8 (without BOM) - use the unicode mode flags to change this. When reading without an explicit unicode mode flag, the content of the file is examined and a guess is made whether the file is UTF8, UTF16 or ANSI. If opening an existing file and that file has a BOM then the BOM will be honored regardless of the unicode mode flags passed.
Opening a file in write mode creates the file if it does not exist. Directories are not created unless the correct flag is used.
When reading and writing via the same file handle, the FileSetPos() function must be used to update the current file position.
A file can be read as binary (byte) data by using FileOpen() with the binary flag.
See "Unicode Support" for a detailed description.
FileClose, FileFlush, FileGetPos, FileRead, FileReadLine, FileSetPos, FileWrite, FileWriteLine
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Example()
Func Example()
; Create a constant variable in Local scope of the filepath that will be read/written to.
Local Const $sFilePath= _WinAPI_GetTempFileName (@TempDir )
; Create a temporary file to read data from.
If Not FileWrite ($sFilePath,"This is an example of using FileOpen.")Then
MsgBox ($MB_SYSTEMMODAL,"","An error occurred whilst writing the temporary file.")
Return False
EndIf
; Open the file for reading and store the handle to a variable.
Local $hFileOpen= FileOpen ($sFilePath,$FO_READ)
If $hFileOpen= - 1Then
MsgBox ($MB_SYSTEMMODAL,"","An error occurred when reading the file.")
Return False
EndIf
; Read the contents of the file using the handle returned by FileOpen.
Local $sFileRead= FileRead ($hFileOpen)
; Close the handle returned by FileOpen.
FileClose ($hFileOpen)
; Display the contents of the file.
MsgBox ($MB_SYSTEMMODAL,"","Contents of the file:"&@CRLF &$sFileRead)
; Delete the temporary file.
FileDelete ($sFilePath)
EndFunc ;==>Example
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Example()
Func Example()
; Create a constant variable in Local scope of the filepath that will be read/written to.
Local Const $sFilePath= _WinAPI_GetTempFileName (@TempDir )
; Open the file for read/write access.
Local $hFileOpen= FileOpen ($sFilePath,$FO_READ+ $FO_OVERWRITE)
If $hFileOpen= - 1Then
MsgBox ($MB_SYSTEMMODAL,"","An error occurred when reading/writing the file.")
Return False
EndIf
; Write some data.
FileWrite ($hFileOpen,"This is some data to show that the handle was open with read/write access."&@CRLF )
; Retrieve the current position in the file.
Local $iFilePos= FileGetPos ($hFileOpen)
; Now, adjust the position to the beginning.
FileSetPos ($hFileOpen,0,$FILE_BEGIN)
; Display the contents of the file.
MsgBox ($MB_SYSTEMMODAL,"",FileRead ($hFileOpen))
; Now, adjust the position back to the previous position.
FileSetPos ($hFileOpen,0,$iFilePos)
; Write some addition data.
FileWrite ($hFileOpen,"This is some additional data.")
; Adjust the position back to the previous position.
FileSetPos ($hFileOpen,0,$FILE_BEGIN)
; Display the contents of the file.
MsgBox ($MB_SYSTEMMODAL,"",FileRead ($hFileOpen))
; Close the handle returned by FileOpen.
FileClose ($hFileOpen)
; Delete the temporary file.
FileDelete ($sFilePath)
Return True
EndFunc ;==>Example