如何当文本写入完成关闭后再读取
我首先往一个文本中写入内容,当写入完成后关闭,然后再读取出来,但是我的脚本的输出总是为空,我的脚本如下:#include <File.au3>#include<array.au3>
Global $x
If $cmdline = 1 Then
$x = $cmdline
EndIf
$file2 = FileOpen(FileGetLongName($x), 0)
If $file2 = -1 Then
MsgBox(0, "错误", "不能打开文件.")
Exit
EndIf
$file2 = FileRead($file2)
FileClose($file2)
FileDelete(@ScriptDir & "\输出1.txt")
$file3 = FileOpen(@ScriptDir & "\输出1.txt", 2)
If $file3 = -1 Then
MsgBox(0, "错误", "不能打开输出文件.")
Exit
EndIf
FileDelete(@ScriptDir & "\输出.txt")
$file = FileOpen(@ScriptDir & "\输出.txt", 2)
If $file = -1 Then
MsgBox(0, "错误", "不能打开输出文件.")
Exit
EndIf
Local $Result,$line, $_line, $str
$Reg = StringRegExp(@CRLF & $file2, '\n\s*[^\d](\d{2,4}\s+.+)|\(OCC\).+', 3)
For $i = 0 To UBound($Reg) - 1
$Result &= $Reg[$i] & @CRLF
Next
FileWrite($file3, $Result)
$close=FileClose($file3)
If $close=1 Then
$file = FileRead($file3)
MsgBox(0,0,$file )
EndIf
这个脚本输出的msgbox为空,但是文本$file3又是正确的,请问是怎么回事呢? $file3 是覆写的模式打开的,当然不能读。要读取的话直接将 43 行改为 $file = FileRead(@ScriptDir & "\输出1.txt")
也可以这样:If $cmdline = 0 Then Exit
Local $file = @ScriptDir & "\输出1.txt"
Local $Reg, $Result, $str
$str = FileRead(FileGetLongName($cmdline))
FileDelete($file)
$Reg = StringRegExp(@CRLF & $str, '\n\s*[^\d](\d{2,4}\s+.+)|\(OCC\).+', 3)
For $i = 0 To UBound($Reg) - 1
$Result &= $Reg[$i] & @CRLF
Next
FileWrite($file, $Result)
MsgBox(0, 0, FileRead($file)) 多谢afan,但是我现在用了filereadline,发现老只读第一行的内容,请问是怎么回事呢?脚本如下:If $cmdline = 0 Then Exit
Local $file = @ScriptDir & "\temp.txt"
Local $Reg, $Result, $str
$str = FileRead(FileGetLongName($cmdline))
FileDelete($file)
$Reg = StringRegExp(@CRLF & $str, '\n\s*[^\d](\d{2,4}\s+.+)|\(OCC\).+', 3)
For $i = 0 To UBound($Reg) - 1
$Result &= $Reg[$i] & @CRLF
Next
FileWrite($file, $Result)
MsgBox(0, 0, FileRead($file))
Local $line, $_line, $str
While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
MsgBox(0,0,$line)
$_line = StringRegExp($line, '^(\(OCC\).+?)\h+, 3)
If Not @error Then
If Not IsDeclared('_' & $_line) Then
Assign('_' & $_line, 0)
$str &= $line & @CRLF
EndIf
Else
$str &= $line & @CRLF
EndIf
WEnd 多谢afan,但是我现在用了filereadline,发现老只读第一行的内容,请问是怎么回事呢?脚本如下:
jchang 发表于 2010-5-8 15:30 http://www.autoitx.com/images/common/back.gif
需要先FileOpen,然后在循环中filereadline会自动一行行读取。
要不就直接filereadline指定行号。不过效率会低很多,不推荐
页:
[1]