FileSetPos
设置当前文件的位置.
FileSetPos ( "filehandle", offset, origin )
参数
filehandle
|
先前调用 FileOpen() 函数返回的文件句柄
|
offset
|
从起点的偏移量. 可以是正数或负数. 负值向后移动
|
origin
|
必须是下列值之一:
$FILE_BEGIN (0) = 文件开始
$FILE_CURRENT (1) = 当前位置
$FILE_END (2) = 文件结尾
常量定义在 FileConstants.au3
|
返回值
成功:
|
返回 True, 操作成功.
|
失败:
|
返回 False.
|
备注
包含 Constants.au3 文件到脚本, 使用"起点"参数括号内的常量名指定操作的起点.
可以对同一文件执行读与写操作. 读,写同一个文件时, 读,写之间必须调用 FileFlush() 函数.
移动指针到数据的中间可用于修改数据.
相关
FileFlush, FileGetPos, FileOpen, FileRead, FileReadLine, FileWrite, FileWriteLine
函数示例
#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
----------------------------------------
|