FileWrite

首页  后退  前进

FileWrite
down2

FileWrite

写文本/数据到先前打开的文件.

 

FileWrite ( "filehandle/filename", "text/data" )

参数

filehandle/filename

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

text/data

写入到文件中的文本/数据. 写入文本时不会自动添加 @CR 或 @LF. 参见数据类型备注.

返回值

成功:

返回 1.

失败:

返回, 文件不是写模式打开, 文件为只读或者无法写入.

备注

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

 

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

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

 

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

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

 

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

 

如果写入数据是二进制类型的变体(而不是文本), 将一个字节一个字节的写入. 二进制操作使用 FileOpen() 函数并强制附带二进制标志.

 

函数示例

#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 FileWrite 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.
    FileWrite($hFileOpen, "Line 2")
    FileWrite($hFileOpen, "This is still line 2 as a new line wasn't appended to the last FileWrite call." & @CRLF)
    FileWrite($hFileOpen, "Line 3" & @CRLF)
    FileWrite($hFileOpen, "Line 4")
    ; 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 调用

参见:

FileOpen, FileRead, FileReadLine, FileWriteLine

例子
copy

exect=$var_h=FileOpen('c:\Test\1.txt',1)||FileWrite($var_h,'字符串1'&@CRLF&'字符串2')||FileClose($var_h) GLOBALEXECT<a> ;; 写入多行文本文件末尾的示例

 

exect=$var_sp='c:\Test\1.txt'||$var_st=FileRead('$var_sp')||$var_h=FileOpen('$var_sp',2)||FileWrite($var_h,'字符串1'&'$var_st')||FileClose($var_h) GLOBALEXECT<a> ;; 写入文件开头的示例

 

exect=$var_s=##[input=BOX72]||$var_ap=_ArrayPermute(StringSplit('$var_s','"''"',2))||_ViewValues($var_ap)||$var_ss=_ArrayToString($var_ap,@CRLF,1)||_ViewValues('$var_ss ')||FileWrite('c:\Test\anagam.txt','$var_ss') ;; 在对话框中输入的单词中创建一个卦,在每个阶段查看结果,并保存到anagam.txt文件中(卦语包含单词,排列给定单词的所有字母,而不重复字母和确切长度)

exect=$var_s=##[input=BOX72]||FileWrite('c:\Test\anagam.txt',_ArrayToString(_ArrayPermute(StringSplit('$var_s','"''"',2)),@CRLF,1)) ;; 在对话框中输入的单词中创建一个卦,并保存到anagam.txt文件

 

exect=$var_s=##[input=BOX72]||FileWrite('c:\Test\anagam_'&'$var_s'&'.txt',_ArrayToString(_ArrayPermute(StringSplit('$var_s','"''"',2)),@CRLF,1)) ;;在对话框中输入的单词中创建一个卦,并保存到文件anagam_word.txt

up2

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