找回密码
 加入
搜索
查看: 2404|回复: 3

[AU3基础] 如何当文本写入完成关闭后再读取

[复制链接]
发表于 2010-5-6 22:50:31 | 显示全部楼层 |阅读模式
我首先往一个文本中写入内容,当写入完成后关闭,然后再读取出来,但是我的脚本的输出总是为空,我的脚本如下:
#include <File.au3>
#include<array.au3>

Global $x
If $cmdline[0] = 1 Then
        $x = $cmdline[1]
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又是正确的,请问是怎么回事呢?
发表于 2010-5-6 23:23:30 | 显示全部楼层
$file3 是覆写的模式打开的,当然不能读。要读取的话直接将 43 行改为 $file = FileRead(@ScriptDir & "\输出1.txt")

也可以这样:
If $cmdline[0] = 0 Then Exit
Local $file = @ScriptDir & "\输出1.txt"
Local $Reg, $Result, $str
$str = FileRead(FileGetLongName($cmdline[1]))
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))
 楼主| 发表于 2010-5-8 15:30:20 | 显示全部楼层
多谢afan,但是我现在用了filereadline,发现老只读第一行的内容,请问是怎么回事呢?脚本如下:
If $cmdline[0] = 0 Then Exit
Local $file = @ScriptDir & "\temp.txt"
Local $Reg, $Result, $str
$str = FileRead(FileGetLongName($cmdline[1]))
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[0]) Then
                        Assign('_' & $_line[0], 0)
                        $str &= $line & @CRLF
                EndIf
        Else
                $str &= $line & @CRLF
        EndIf
WEnd
发表于 2010-5-8 17:53:09 | 显示全部楼层
多谢afan,但是我现在用了filereadline,发现老只读第一行的内容,请问是怎么回事呢?脚本如下:
jchang 发表于 2010-5-8 15:30



    需要先FileOpen,然后在循环中filereadline会自动一行行读取。
要不就直接filereadline指定行号。不过效率会低很多,不推荐
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-22 07:41 , Processed in 0.073049 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表