函数参考


FileReadLine

从此前已打开的文本文件中读取指定行的字符.

FileReadLine ( "文件句柄/文件名" [, 行号] )

参数

文件句柄 目标文件句柄,可由此前调用 FileOpen 函数的返回值获得.另外也可以直接使用目标文件名.
行号 [可选参数] 要读取的行号.第一行用1表示(不是0).

返回值

成功: 返回指定行的文本.
特殊: 把 @error 设为 -1,说明已经读到文件尾.
失败: 把 @error 设为 1,说明文件并非以读取模式打开或者有其它错误.

注意/说明

本函数将返回读到的指定行的文本内容,每一"行"之间都是以新行符分割( CHR(10) 或 @LF ).
若没有给定要读取的行号,则程序将自动读取"下一行"("下一行"对新打开的文件而言是指第一行).
If a filename is given rather than a file handle - the file will be opened and closed during the function call - for parsing large text files this will be much slower than using filehandles.
Note: Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Use either filehandles or filenames in your routines, not both!

From a performance standpoint it is a bad idea to read line by line specifying "line" parameter whose value is incrementing by one. This forces AutoIt to reread the file from the beginning until it reach the specified line.

Both ANSI and UTF16/UTF8 text formats can be read - AutoIt will automatically determine the type.

相关

IniRead, FileOpen, FileRead, FileWrite, FileWriteLine, FileSetPos, FileGetPos

示例/演示


;要打开的文件
Local $file = FileOpen("test.txt", 0)

; 检查打开的文件是否可读
If $file = -1 Then
    MsgBox(4096, "错误", "不能打开文件.")
    Exit
EndIf

; 每次读取一行文本,直到文件结束.
While 1
    Local $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(4096, "读取的行:", $line)
WEnd

FileClose($file)