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:
    $FILE_BEGIN (0) = Beginning of the file.
    $FILE_CURRENT (1) = Current position.
    $FILE_END (2) = End of the file.

Constants are defined in FileConstants.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

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

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 writing (overwrite the file) and store the handle to a variable.
        Local $hFileOpen = FileOpen($sFilePath, $FO_OVERWRITE)
        If $hFileOpen = -1 Then
                MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
                Return False
        EndIf

        ; Write data to the file using the handle returned by FileOpen.
        FileWriteLine($hFileOpen, "Line 1")
        FileWriteLine($hFileOpen, "Line 2")
        FileWriteLine($hFileOpen, "Line 3")

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

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

        ; Now, adjust the position to the beginning.
        FileSetPos($hFileOpen, 0, $FILE_BEGIN)

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

        ; Close the handle returned by FileOpen.
        FileClose($hFileOpen)

        ; Delete the temporary file.
        FileDelete($sFilePath)
EndFunc   ;==>Example