FileOpen

首页  后退  前进

FileOpen
down2

FileOpen

打开文件以供读取或写入.

 

FileOpen ( "filename" [, mode = 0] )

参数

filename

要打开文件的文件名.

mode

[可选] 文件打开模式.

可以是以下值的组合:

   $FO_READ (0) = 只读模式(默认)

   $FO_APPEND (1) = 写入模式(追加数据到文件尾部)

   $FO_OVERWRITE (2) = 写入模式(删除之前的内容)

   $FO_CREATEPATH (8) = 如果目标目录不存在就创建(见备注).

   $FO_BINARY (16) = 强制二进制模式(见备注).

   $FO_UNICODE 或 $FO_UTF16_LE (32) = 使用 Unicode UTF16 小编码读写模式.

   $FO_UTF16_BE (64) = 使用 Unicode UTF16 大编码读写模式.

   $FO_UTF8 (128) = 使用 Unicode UTF8 (带 BOM)编码读写模式).

   $FO_UTF8_NOBOM (256) = 使用 Unicode UTF8 (无 BOM)编码读写模式).

   $FO_ANSI (512) = 使用 ANSI 编码读写模式.

   $FO_UTF16_LE_NOBOM (1024) = 使用 Unicode UTF16 (无 BOM)小编码读写模式.

   $FO_UTF16_BE_NOBOM (2048) = 使用 Unicode UTF16 (无 BOM)大编码读写模式.

   $FO_FULLFILE_DETECT (16384) = 读取没有 BOM 的打开文件时, 使用完整文件检测来确定是 UTF8 或 UTF16. 如果不使用则由于性能原因只会检测文件的初始部分 (64KB).

文件夹路径必须存在(除非指定模式 $FO_CREATEPATH (8) - 见备注).

 

常量定义在 FileConstants.au3

返回值

成功:

返回文件句柄, 供随后的文件函数调用.

失败:

返回 -1, 发生错误.

备注

文件句柄必须由 FileClose() 函数关闭.

 

文件可能因为访问权限或者属性而无法打开.

 

默认写入模式为 UTF8 (无 BOM) - 可以使用 unicode 模式标志来更改. 当不带明确 unicode 模式标志读取时, 会检查文件内容并猜测文件是 UTF8, UTF16 或 ANSI 模式. 如果打开的文件有 BOM , 则无聊检测为什么模式都将尊从 BOM.

 

写模式打开文件时, 如果它不存在, 文件会被创建; 但不会创建目录, 除非使用正确的标志.

 

同时读写一个相同的文件句柄, 必须使用 FileSetPos() 函数更新当前文件位置.

 

可以设置本函数标志读取二进制(字节).

 

"Unicode 支持" 中的说明.

 

函数示例

示例 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)
    ; Create a temporary file to read data from.
    If Not FileWrite($sFilePath, "This is an example of using FileOpen.") Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")
        Return False
    EndIf
    ; Open the file for reading and store the handle to a variable.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
    EndIf
    ; Read the contents of the file using the handle returned by FileOpen.
    Local $sFileRead = FileRead($hFileOpen)
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead)
    ; 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 read/write access.
    Local $hFileOpen = FileOpen($sFilePath, $FO_READ + $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading/writing the file.")
        Return False
    EndIf
    ; Write some data.
    FileWrite($hFileOpen, "This is some data to show that the handle was open with read/write access." & @CRLF)
    ; Retrieve the current position in the file.
    Local $iFilePos = FileGetPos($hFileOpen)
    ; Now, adjust the position to the beginning.
    FileSetPos($hFileOpen, 0, $FILE_BEGIN)
    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", FileRead($hFileOpen))
    ; Now, adjust the position back to the previous position.
    FileSetPos($hFileOpen, 0, $iFilePos)
    ; Write some addition data.
    FileWrite($hFileOpen, "This is some additional data.")
    ; Adjust the position back to the previous position.
    FileSetPos($hFileOpen, 0, $FILE_BEGIN)
    ; Display the contents of the file.
    MsgBox($MB_SYSTEMMODAL, "", FileRead($hFileOpen))
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    ; Delete the temporary file.
    FileDelete($sFilePath)
    Return True
EndFunc   ;==>Example

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

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

参见:

FileClose, FileRead, FileReadLine, FileWrite, FileWriteLine

例子
copy

exect=$var_h=FileOpen('c:\Test\1.txt',16)||$var_b=FileRead($var_h,5)||FileClose($var_h) GLOBALEXECT<a> ;; 从文件读取5个字节的示例

 

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

 

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

 

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_h=FileOpen('c:\Test\1.txt')||FileReadLine($var_h,5)||FileClose($var_h) GLOBALEXECT<a> ;; 从文本文件读取第5行的示例

 

exect=$var_h=FileOpen('%P%N',16)||$var_s=BinaryToString(FileRead($var_h),4)||FileClose($var_h)||_ViewValues('$var_s') ;; 以UTF-8编码读取光标下的文件数据的示例

up2

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