FileWriteLine

首页  后退  前进

FileWriteLine
down2

FileWriteLine

添加一行文本到打开的文本文件尾部.

 

FileWriteLine ( "filehandle/filename", "line" )

参数

filehandle/filename

由此前 FileOpen() 函数返回的文件句柄. 也可以直接使用目标文件名.

line

准备写入的文本. 如果文本不以字符 @CR 或 @LF 结束, 则自动添加 DOS 换行符(@CRLF).

返回值

成功:

返回 1.

失败:

返回 0, 文件无法以写入模式打开, 文件为只读属性或无法写入.

备注

文本文件必须以写模式打开, 否则本函数命令失败.

 

若指定文件名而不是文件句柄, 文件将在函数执行期间被打开并关闭.

如果打开的文件较大, 读取会比使用文件句柄要慢得多. 如果该文件不存在, 则函数会自动创建该文件.

 

注意:不要混用文件的句柄和名称, 即用 FileOpen() 函数打开文件,

又在本函数中使用文件名. 二者只取其一, 不要两者都使用.

 

写入文本时, AutoIt 默认使用 UTF8 (无 BOM)  模式. 写入其他模式文本, 必须使用 FileOpen() 设置相关标志.

 

写入文本不能包含 Chr(0) 字符, 此字符输出时会被截断.

编写这样的字符使用 FileWrite() 二进制模式打开文件.

 

函数示例

#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)
    ; Create a temporary file to write data to.
    If Not FileWrite($sFilePath, "Start of the FileWriteLine example, line 1. " & @CRLF) Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf
    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
    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 2")
    FileWriteLine($hFileOpen, "This is line 3 as a new line was appended to the last FileWriteLine call." & @CRLF)
    FileWriteLine($hFileOpen, "Line 4" & @CRLF)
    FileWriteLine($hFileOpen, "Line 5")
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    ; Display the contents of the file passing the filepath to FileRead instead of a handle returned by FileOpen.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & FileRead($sFilePath))
    ; Delete the temporary file.
    FileDelete($sFilePath)
EndFunc   ;==>Example

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

 

该函数可以通过命令 exect 调用

参见:

FileReadLine, FileOpen, FileRead, FileWrite

例子
copy

exect=$var_h=FileOpen('c:\Test\1.txt',1)||FileWriteLine($var_h,'字符串1')||FileClose($var_h) ;; 写入文件末尾的例子

up2

tcimage © Аверин Андрей для Total Commander Image Averin-And@yandex.ru