FileRead

首页  后退  前进

FileRead
down2

FileRead

读取先前打开文本文件中指定数量的字符.

 

FileRead ( "filehandle/filename" [, count] )

参数

filehandle/filename

由此前 FileOpen() 函数返回的文件句柄. 也可以使用文件名字符串作为参数.

count

[可选] 读取的字符数. 见备注.

返回值

成功:

返回读取的字符/二进制数据. @extended 返回读取的字节/字符数量.

失败:

@error 设置 为非 0 值.

@error:

1 = 文件不是只读模式打开或其它错误

-1 = 已读到文件尾.

备注

负值或未定义"count", 从当前位置读取整个文件.

 

如果指定的是文件名而不是文件句柄 - 文件将被打开, 并在函数调用期间关闭.

解析大的文本文件会比使用文件句柄慢许多.

注意: 不要混合使用文件句柄和文件名, 即不要在使用 FileOpen() 打开文件后,

又在函数中使用文件名. 不能同时使用文件名和文件句柄!

 

可以读取 ANSI 和 UTF16/UTF8 格式 - 见 FileOpen() 详情.

 

可以设置 FileOpen() 函数标志读取二进制(字节), 此时返回计数是字节数而不是字符数.

过大的 "count" 可能导致 Autoit 停止和内存分配失败.

 

函数示例

#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 FileRead.") 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

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

 

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

参见:

FileOpen, FileReadLine, FileWrite, FileWriteLine

例子
copy

exect=FileRead('D:\TC~~IMAGE\test.htm') GLOBALEXECT<a> ;; 读取文件的示例

 

exect=FileRead(EnvGet('COMMANDER_PATH')&'\WinButtonBar.ini') GLOBALEXECT<a> ;; 使用相对路径读取文件的示例

 

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

 

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_a1=StringSplit(FileRead('%P%N'),'~~',1) GLOBALEXECT<a> ;; 空格一个将文本文件分解成数组的例子

 

exect=_ViewValues(FileRead('%P%N'),'%P%N') ;; 查看光标下的文件内容

 

exect=_ViewValues(FileRead('%P%N'),'%P%N',@DesktopWidth,@DesktopHeight) ;; 查看光标下的文件内容,窗口全屏

 

exect=_ViewValues(FileRead('%L'),'"%P"',600,500,0,0,-1,14,'FFFF00','050000') ;; 使用不同的窗口设置查看所选对象的列表

 

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

 

exect=$var_s=##[input=BOX73]||$var_ar=StringRegExp(FileRead(@CRLF&'C:\dictionary.txt'&@CRLF),'[\r\n](['&'$var_s'&']+)[\r\n]',3)||_ViewValues($var_ar)||$var_s=_ArrayToString($var_ar,@CRLF) bufsr=##$var_s ;; 发送到剪贴板的所有单词组成的字母输入在对话框中,这是字典dictionary.txt的一部分(该列表包含单词,排列所有给定的集合字母重复的字母没有确切的长度)

exect=$var_s=##[input=BOX73]||$var_ar=StringRegExp(@CRLF&FileRead('C:\dictionary.txt')&@CRLF,'[\r\n](['&'$var_s'&']{3,6})[\r\n]',3)||_ViewValues($var_ar)||$var_s=_ArrayToString($var_ar,@CRLF) bufsr=##$var_s ;; 发送到剪贴板的所有单词形成从字母dictionary.txt的一部分在对话框中输入的字母(该列表包括字母,排列所有字母的重复的字母和长度为5个字符)

exect=$var_s=##[input=BOX73]||$var_ar=StringRegExp(@CRLF&FileRead('C:\dictionary.txt')&@CRLF,'[\r\n](['&'$var_s'&']{5})[\r\n]',3)||_ViewValues($var_ar)||$var_s=_ArrayToString($var_ar,@CRLF) bufsr=##$var_s ;;发送到剪贴板的所有单词组成的字母在字典dictionary.txt的对话框中输入的字母(该列表包括字,排列所有的字母,重复的字母和长度从3到6个字符)

exect=$var_s=##[input=BOX73]||$var_ar=StringRegExp(@CRLF&FileRead('C:\dictionary.txt')&@CRLF,'[\r\n](['&'$var_s'&']{[input=BOX74],[input=BOX75]})[\r\n]',3)||_ViewValues($var_ar)||$var_s=_ArrayToString($var_ar,@CRLF) bufsr=##$var_s ;; 发送到剪贴板的所有单词组成的字母在对话框中输入的字典dictionary.txt的一部分(该列表包括单词,排列所有字母的重复的字母和长度在对话框中设置)

exect=_ViewValues(StringRegExp(FileRead('%P%N'),'(?m)^([^\r\n]*)',3),'%P%N') ;; 在数组行和视图数组中将文件的内容拆分为光标

 

exect=$var_a=StringSplit(FileRead('%P%N'),'''',2)||$var_a=_ArrayUnique($var_a) asave=var_a||"%P%O_sym.%E"||upd<1>||all|| ;; 使用_sym前缀写入光标下的文件内容的唯一文件

 

exect=$var_s=FileRead('%P%N')||$var_s=StringReplace('$var_s',@CRLF,'''')||$var_s=StringReplace('$var_s','~~','''')||$var_a=StringSplit('$var_s','''',2)||$var_a=_ArrayUnique($var_a)||_ArraySort($var_a,0,1) asave=var_a||"%P%O_sym.%E"||upd<1>||all||||code<1> ;; 使用_sym前缀写入ns文件,在UTF-16 LE 1200编码中的光标下的文件的唯一内容,删除换行符和空格后

 

exect=$var_a=StringRegExp(FileRead('%P%N'),'(?ms)@font-face(?:[\s]+)?\{[^\{]+\}',3)||_ViewValues($var_a) ;; 在游标下分割成数组文件css所有的片段@ font-face

exect=$var_a=StringRegExp(FileRead('%P%N'),'(?ms)@font-face(?:[\s]+)?\{[^\{]+\}',3)||_ViewValues($var_a)||$var_a=_ArrayUnique($var_a)||_ViewValues($var_a) ;; 在游标下分割成数组css文件所有的片段@ font-face和删除重复的片段

 

sends=\<link\h+rel\=[\''|\']?[^>]+\>??1 exect=$var_a=_ArrayUnique(StringRegExp(FileRead('%P%N'),$GLOBALSENDS[1],3))||_ViewValues($var_a) ;; 在光标下获取指定的文件段,并在表中显示它们

 

exect=$var_a=StringSplit(FileRead('%P%N'),'</p><p>',1)||_ViewValues($var_a) ;; 将光标下的文本文件用短语"</ p> <p>"

 

exect=$var_a=StringRegExp(FileRead('%P%N'),'(?m)((?:[^\r\n]+(?:\r\n|$)+){5})',3)||_ViewValues($var_a) ;; 将光标下的文本文件拆分5行

 

slist=%L exect=$var_a=StringRegExp(FileRead('GLOBALSLIST<t>'),'(?m)((?:[^\r\n]+(?:\r\n|$)+){5})',3) GLOBALEXECT<a> ;; 将所选对象的路径分为5组

 

slist=%L exect=$var_a=StringRegExp(FileRead('GLOBALSLIST<t>'),'(?m)((?:[^\r\n]+(?:\r\n|$)+){1,5})',3) GLOBALEXECT<a> ;; 将所选对象的路径分成5 +的余数(如果分配不是5的倍数)

up2

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