Function Reference

首页  后退  前进

FileSetEnd

 

设置当前文件的位置到文件末尾.

 

FileSetEnd ( "filehandle" )

参数

filehandle

先前调用 FileOpen() 函数返回的文件句柄

返回值

成功:

返回 True 操作成功

失败:

返回 False

相关

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

函数示例

示例 1

#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)
    ; 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))
    ; Write data to the file using the handle returned by FileOpen.
    FileWriteLine($hFileOpen, "Line 4")
    FileWriteLine($hFileOpen, "Line 5")
    FileWriteLine($hFileOpen, "Line 6")
    ; 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))
    ; Now, adjust the position to the beginning.
    FileSetPos($hFileOpen, 0, $FILE_BEGIN)
    ; Set the end of the file at the current position.
    FileSetEnd($hFileOpen)
    ; Check file position and try to read contents for current position. The contents have been destroyed.
    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

示例 2

#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 and set the end of the file.
    FileWrite($hFileOpen, "ABCDEF")
    FileSetEnd($hFileOpen)
    ; Display the file size and contents.
    MsgBox($MB_SYSTEMMODAL, "", "Size: " & FileGetSize($sFilePath) & @CRLF & "Data: " & @CRLF & FileRead($sFilePath))
    ; Expand the size of the file to 9 bytes and don't write any additional data.
    FileSetPos($hFileOpen, 9, $FILE_BEGIN)
    FileSetEnd($hFileOpen)
    ; Display the file size and contents.
    MsgBox($MB_SYSTEMMODAL, "", "Size: " & FileGetSize($sFilePath) & @CRLF & "Data: " & @CRLF & FileRead($sFilePath))
    ; Move the position after ABCDEF i.e. 6 bytes and then write additional data which will expand the file to 12 bytes.
    FileSetPos($hFileOpen, 6, $FILE_BEGIN)
    FileWrite($hFileOpen, "HIJKLM")
    FileSetEnd($hFileOpen) ; Set the end of the file.
    ; Display the file size and contents.
    MsgBox($MB_SYSTEMMODAL, "", "Size: " & FileGetSize($sFilePath) & @CRLF & "Data: " & @CRLF & FileRead($sFilePath))
    ; Truncate the file size to 9 bytes.
    FileSetPos($hFileOpen, 9, $FILE_BEGIN)
    FileSetEnd($hFileOpen) ; Set the end of the file.
    ; Display the file size and contents.
    MsgBox($MB_SYSTEMMODAL, "", "Size: " & FileGetSize($sFilePath) & @CRLF & "Data: " & @CRLF & FileRead($sFilePath))
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    ; Delete the temporary file.
    FileDelete($sFilePath)
EndFunc   ;==>Example

----------------------------------------