Function Reference


_WinAPI_SetFilePointer

Show description in

Moves the file pointer of the specified file

#include <WinAPIFiles.au3>
_WinAPI_SetFilePointer ( $hFile, $iPos [, $iMethod = 0] )

Parameters

$hFile Handle to the file to be processed
$iPos Number of bytes to move the file pointer. Maximum value is 2^32
A positive value moves the file pointer forward in the file, and a negative value moves the file pointer back.
$iMethod [optional] The starting point for the file pointer move.
Can be one of the predefined values:
    $FILE_BEGIN (0) = (default) The starting point is zero (0) or the beginning of the file
    $FILE_CURRENT (1) = The starting point is the current value of the file pointer.
    $FILE_END (2) = The starting point is the current end-of-file position.

Return Value

Success: New file pointer.
Failure: (-1) INVALID_SET_FILE_POINTER, call _WinAPI_GetLastError() to get extended error information

Remarks

This function can also be used to query the current file pointer position by specifying a move method of FILE_CURRENT and a distance of zero.
This function stores the file pointer in LONG value. To work with file pointers that are larger than a single LONG value, it must be used the SetFilePointerEx function.
File pointer is the position in the file to read/write to/from by _WinAPI_ReadFile()/_WinAPI_WriteFile()

Related

_WinAPI_CloseHandle, _WinAPI_CreateFile, _WinAPI_FlushFileBuffers, _WinAPI_GetFileSizeEx, _WinAPI_ReadFile, _WinAPI_SetEndOfFile, _WinAPI_WriteFile

See Also

Search SetFilePointer in MSDN Library.

Example

#include <WinAPIFiles.au3>
#include <WinAPIHObj.au3>

Local $sTempFile, $hFile, $sText, $nBytes, $tBuffer, $iSize

; 1) create file and write data to it
$sTempFile = @TempDir & '\test.txt'
$sText = 'abcdefghijklmnopqrstuvwxyz'
$tBuffer = DllStructCreate("byte[" & StringLen($sText) & "]")
DllStructSetData($tBuffer, 1, $sText)
$hFile = _WinAPI_CreateFile($sTempFile, 1)
_WinAPI_WriteFile($hFile, $tBuffer, StringLen($sText), $nBytes)
$iSize = _WinAPI_GetFileSizeEx($hFile)
_WinAPI_CloseHandle($hFile)
ConsoleWrite('1):' & $iSize & ' ' & FileRead($sTempFile) & @CRLF)

; 2) read 6 bytes from position 3
$tBuffer = DllStructCreate("byte[6]")
$hFile = _WinAPI_CreateFile($sTempFile, 2, 2)
$iSize = _WinAPI_GetFileSizeEx($hFile)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_ReadFile($hFile, $tBuffer, 6, $nBytes)
_WinAPI_CloseHandle($hFile)
$sText = BinaryToString(DllStructGetData($tBuffer, 1))
ConsoleWrite('2):' & $iSize & ' ' & $sText & @CRLF)

; 3) write previously read 6 bytes from position 3 to the same position but in UpperCase
DllStructSetData($tBuffer, 1, StringUpper($sText))
$hFile = _WinAPI_CreateFile($sTempFile, 2, 4)
$iSize = _WinAPI_GetFileSizeEx($hFile)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_WriteFile($hFile, $tBuffer, 6, $nBytes)
_WinAPI_CloseHandle($hFile)
$tBuffer = 0
ConsoleWrite('3):' & $iSize & ' ' & FileRead($sTempFile) & @CRLF)

; 4) truncate file size to 12 bytes
$hFile = _WinAPI_CreateFile($sTempFile, 2, 4)
_WinAPI_SetFilePointer($hFile, 12)
_WinAPI_SetEndOfFile($hFile)
$iSize = _WinAPI_GetFileSizeEx($hFile)
_WinAPI_CloseHandle($hFile)
ConsoleWrite('4):' & $iSize & ' ' & FileRead($sTempFile) & @CRLF)

FileDelete($sTempFile)