Opens a file to read specific content from it and/or to write new content into it.
file := FileOpen(Filename, Flags , Encoding)
The path of the file to open, which is assumed to be in A_WorkingDir if an absolute path isn't specified.
[v1.1.17+]: Specify an asterisk (or two) as shown below to open the standard input/output/error stream:
FileOpen("*", "r") ; for stdin FileOpen("*", "w") ; for stdout FileOpen("**", "w") ; for stderr
Either [in AHK_L 54+] a string of characters indicating the desired access mode followed by other options (with optional spaces or tabs in between); or [in AHK_L 42+] a combination (sum) of numeric flags. Supported values are described in the tables below.
The code page to use for text I/O if the file does not contain a UTF-8 or UTF-16 byte order mark, or if the h (handle) flag is used. If omitted, the current value of A_FileEncoding is used.
h
r
w
d
-
-rwd
`r`n
`n
`r
If the file is opened successfully, the return value is a File object.
If the function fails, the return value is 0 and [in AHK_L 54+] A_LastError contains an error code.
Use if file or IsObject(file) to check if the function succeeded.
if file
IsObject(file)
When a UTF-8 or UTF-16 file is created, a byte order mark is written to the file unless Encoding (or A_FileEncoding if Encoding is omitted) contains UTF-8-RAW or UTF-16-RAW.
UTF-8-RAW
UTF-16-RAW
When a file containing a UTF-8 or UTF-16 byte order mark (BOM) is opened with read access, the BOM is excluded from the output by positioning the file pointer after it. Therefore, File.Position may report 3 or 2 immediately after opening the file.
File.Position
FileEncoding, File Object, FileRead
Writes some text to a file then reads it back into memory (it provides the same functionality as this DllCall example).
FileSelectFile, FileName, S16,, Create a new file: if (FileName = "") return file := FileOpen(FileName, "w") if !IsObject(file) { MsgBox Can't open "%FileName%" for writing. return } TestString := "This is a test string.`r`n" ; When writing a file this way, use `r`n rather than `n to start a new line. file.Write(TestString) file.Close() ; Now that the file was written, read its contents back into memory. file := FileOpen(FileName, "r-d") ; read the file ("r"), share all access except for delete ("-d") if !IsObject(file) { MsgBox Can't open "%FileName%" for reading. return } CharsToRead := StrLen(TestString) TestString := file.Read(CharsToRead) file.Close() MsgBox The following string was read from the file: %TestString%
Opens the script in read-only mode and read its first line.
file := FileOpen(A_ScriptFullPath, "r") MsgBox % file.ReadLine()
Demonstrates the usage of the standard input/output streams.
; Open a console window for this demonstration: DllCall("AllocConsole") ; Open the application's stdin/stdout streams in newline-translated mode. stdin := FileOpen("*", "r `n") ; Requires [v1.1.17+] stdout := FileOpen("*", "w `n") ; For older versions: ; stdin := FileOpen(DllCall("GetStdHandle", "int", -10, "ptr"), "h `n") ; stdout := FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n") stdout.Write("Enter your query.`n\> ") stdout.Read(0) ; Flush the write buffer. query := RTrim(stdin.ReadLine(), "`n") stdout.WriteLine("Your query was '" query "'. Have a nice day.") stdout.Read(0) ; Flush the write buffer. Sleep 5000