Function Reference


FileSetPos

Sets the current file position.

FileSetPos ( "filehandle", offset, origin )

Parameters

filehandle The handle of a file, as returned by a previous call to FileOpen.
offset The offset to move from the origin. This value may be positive or negative. Negative values move backwards from the origin.
origin Must be one of the following:
 0 - Beginning of the file ($FILE_BEGIN from Constants.au3).
 1 - Current position ($FILE_CURRENT from Constants.au3).
 2 - End of the file ($FILE_END from Constants.au3).

Return Value

Success: True if the operation succeeded.
Failure: False.

Remarks

Include Constants.au3 in your script to use the symbolic name in parentheses to specify the origin.
Using FileSetPos() it is possible to both read and write to the same file. When attempting to read and write to the same file, always call FileFlush() between each write and read operation.
Moving the pointer to the middle of the data can be used to overwrite data.

Related

FileGetPos, FileFlush, FileRead, FileReadLine, FileWrite, FileWriteLine, FileOpen

Example


#include <Constants.au3>

Local Const $sFile = "test.txt"
Local $hFile = FileOpen($sFile, 2)

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

; Write something to the file.
FileWriteLine($hFile, "Line1")
FileWriteLine($hFile, "Line2")
FileWriteLine($hFile, "Line3")

; Flush the file to disk.
FileFlush($hFile)

; Check file position and try to read contents for current position.
MsgBox(0, "", "Position: " & FileGetPos($hFile) & @CRLF & "Data: " & @CRLF & FileRead($hFile))

; Now, adjust the position to the beginning.
Local $n = FileSetPos($hFile, 0, $FILE_BEGIN)

; Check file position and try to read contents for current position.
MsgBox(0, "", "Position: " & FileGetPos($hFile) & @CRLF & "Data: " & @CRLF & FileRead($hFile))

; Close the handle.
FileClose($hFile)

; Clean up the temporary file.
FileDelete($sFile)