File Object [AHK_L 42+]

Provides an interface for file input/output. FileOpen returns an object of this type.

Read

Reads a string of characters from the file and advances the file pointer.

String := File.Read([Characters])
CharactersThe maximum number of characters to read. If omitted, the rest of the file is read and returned as one string.
ReturnsA string.

Write

Writes a string of characters to the file and advances the file pointer.

File.Write(String)
StringA string.
ReturnsThe number of bytes (not characters) that were written.

ReadLine

Reads a line of text from the file and advances the file pointer.

Line := File.ReadLine()
ReturnsA line of text. This may include `n, `r`n or `r depending on the file and EOL flags used to open the file.

WriteLine

Writes a string of characters followed by `n or `r`n depending on the flags used to open the file. Advances the file pointer.

File.WriteLine([String])
StringAn optional string.
ReturnsThe number of bytes (not characters) that were written.

ReadNum

Reads a number from the file and advances the file pointer.

Num := File.ReadNumType()
NumTypeOne of the following specified directly as part of the function name:
UInt, Int, Int64, Short, UShort, Char, UChar, Double, or Float.
ReturnsA number if successful, otherwise an empty string.

WriteNum

Writes a number to the file and advances the file pointer.

File.WriteNumType(Num)
NumTypeOne of the following specified directly as part of the function name:
UInt, Int, Int64, Short, UShort, Char, UChar, Double, or Float.
NumA number.
ReturnsThe number of bytes that were written. For instance, WriteUInt returns 4 if successful.

RawRead

Read raw binary data from the file into memory. If a var is specified, it is expanded automatically when necessary.

File.RawRead(VarOrAddress, Bytes)
VarOrAddressA variable or memory address to which the data will be copied. Usage is similar to NumGet.
BytesThe maximum number of bytes to read.
ReturnsThe number of bytes that were read.

RawWrite

Write raw binary data to the file.

File.RawWrite(VarOrAddress, Bytes)
VarOrAddressA variable containing the data or the address of the data in memory. Usage is similar to NumPut.
BytesThe number of bytes to write.
ReturnsThe number of bytes that were written.

Seek

Moves the file pointer.

File.Seek(Distance [, Origin = 0])
File.Position := Distance
File.Pos := Distance
DistanceDistance to move, in bytes. Lower values are closer to the beginning of the file.
OriginStarting point for the file pointer move. Must be one of the following:
  • 0 (SEEK_SET): Beginning of the file. Distance must be zero or greater.
  • 1 (SEEK_CUR): Current position of the file pointer.
  • 2 (SEEK_END): End of the file. Distance should usually be negative.
If omitted, Origin defaults to SEEK_END when Distance is negative and SEEK_SET otherwise.
ReturnsA non-zero value if successful, otherwise zero.

Tell

Pos := File.Tell()
Pos := File.Position
Pos := File.Pos
ReturnsThe current position of the file pointer, where 0 is the beginning of the file.

Length

Retrieves or sets the size of the file.

FileSize := File.Length
File.Length := NewSize
NewSizeThe new size of the file, in bytes.
ReturnsThe size of the file, in bytes.

AtEOF

IsAtEOF := File.AtEOF
ReturnsA non-zero value if the file pointer has reached the end of the file, otherwise zero.

Close

Closes the file, flushes any data in the cache to disk and releases the share locks. Although the file is closed automatically when the object is freed, it is recommended to close the file as soon as possible.

File.Close()

No parameters or return value.

Encoding

Retrieves or sets the text encoding used by this file object.

Encoding := File.Encoding
File.Encoding := Encoding
EncodingA string in the format accepted by FileEncoding.

__Handle

File.__Handle
ReturnsA system file handle, intended for use with DllCall. See CreateFile.