FileReadLine
读取文本文件指定行的文本.
FileReadLine ( "filehandle/filename" [, line = 1] )
参数
filehandle/filename
|
由此前 FileOpen() 函数返回的文件句柄. 也可以使用文件名字符串作为参数.
|
line
|
[可选] 读取的行号. 文本文件的第一行为 1(不为 0), 最后一行为 -1.
|
返回值
成功:
|
返回指定行的文本.
|
失败:
|
设置 @error 为非 0 值
|
@error:
|
1 = 文件没有以读取模式打开, 在或其它错误.
-1 = 已读到文件尾.
|
备注
函数将返回读取指定行的文本内容, 行末尾的任何换行符 ( Chr(10) 或 @LF ) 自动剥离.
若没有指定行号, 则将自动读取"下一行". ("下一行"对新打开的文件而言是指第一行..)
如果指定的是文件名而不是文件句柄 - 文件将被打开, 并在函数调用期间关闭.
如果打开的文件较大, 读取会比使用文件句柄要慢得多.
注意: 不要混合使用文件句柄和文件名, 即不要在使用 FileOpen() 打开文件后,
又在函数中使用文件名. 不能同时使用文件名和文件句柄!
从性能的角度来看, 一行行读取是一个坏主意, 此时"line"参数是由一逐行递增.
这将迫使 AutoIt 从开始重读该文件, 直至达到指定的行.
ANSI 和 UTF16/UTF8 文本格式都可以读取 - 见 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 read data from.
If Not FileWrite($sFilePath, "This is an example of using FileReadLine.") 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 fist line of the file using the handle returned by FileOpen.
Local $sFileRead = FileReadLine($hFileOpen, 1)
; Close the handle returned by FileOpen.
FileClose($hFileOpen)
; Display the first line of the file.
MsgBox($MB_SYSTEMMODAL, "", "First line of the file:" & @CRLF & $sFileRead)
; Delete the temporary file.
FileDelete($sFilePath)
EndFunc ;==>Example
----------------------------------------
该函数可以通过命令 exect 调用
参见:
FileWriteLine, FileOpen, FileRead, FileWrite
exect=$var_h=FileOpen('c:\Test\1.txt')||FileReadLine($var_h,5)||FileClose($var_h) GLOBALEXECT<a> ;; 从文本文件读取第5行的示例
exect=$var_sline=FileReadLine('c:\Test\1.txt',4) GLOBALEXECT<a> ;; 从文本文件读取第4行的示例
© Аверин Андрей для Total Commander Image Averin-And@yandex.ru
|