移动指定文件的指针
#Include <WinAPI.au3>
_WinAPI_SetFilePointer($hFile, $iPos [, $iMethod = 0])
$hFile | 文件句柄 |
$iPos | 移动文件指针的字节数,最大值为 2^32 正值向前移动文件指针, 而负数则向后移动文件指针. |
$iMethod | [可选参数] 文件指针移动的起点. 可以预定义下列值之一: $FILE_BEGIN = 0 - 起点为 0 或文件的开头 $FILE_CURRENT = 1 - 起点为文件指针的当前值. $FILE_END = 2 - 起点为文件的目前结束位置. 默认值为 $FILE_BEGIN = 0 |
成功: | 返回新的文件指针. |
失败: | 返回 INVALID_SET_FILE_POINTER (-1) 并设置 @error |
@error: | 0 - 无错误 |
2 - API 返回 INVALID_SET_FILE_POINTER |
在MSDN中搜索
#include <WinAPI.au3>
Global $sFile, $hFile, $sText, $nBytes, $tBuffer
; 1) create file and write data to it
$sFile = @ScriptDir & '\test.txt'
$sText = 'abcdefghijklmnopqrstuvwxyz'
$tBuffer = DllStructCreate("byte[" & StringLen($sText) & "]")
DllStructSetData($tBuffer, 1, $sText)
$hFile = _WinAPI_CreateFile($sFile, 1)
_WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), StringLen($sText), $nBytes)
_WinAPI_CloseHandle($hFile)
ConsoleWrite('1) ' & FileRead($sFile) & @CRLF)
; 2) read 6 bytes from position 3
$tBuffer = DllStructCreate("byte[6]")
$hFile = _WinAPI_CreateFile($sFile, 2, 2)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_ReadFile($hFile, DllStructGetPtr($tBuffer), 6, $nBytes)
_WinAPI_CloseHandle($hFile)
$sText = BinaryToString(DllStructGetData($tBuffer, 1))
ConsoleWrite('2) ' & $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($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, 3)
_WinAPI_WriteFile($hFile, DllStructGetPtr($tBuffer), 6, $nBytes)
_WinAPI_CloseHandle($hFile)
$tBuffer = 0
ConsoleWrite('3) ' & FileRead($sFile) & @CRLF)
; 4) truncate file size to 12 bytes
$hFile = _WinAPI_CreateFile($sFile, 2, 4)
_WinAPI_SetFilePointer($hFile, 12)
_WinAPI_SetEndOfFile($hFile)
_WinAPI_CloseHandle($hFile)
ConsoleWrite('4) ' & FileRead($sFile) & @CRLF)