找回密码
 加入
搜索
查看: 2791|回复: 5

[AU3基础] [已解决]一个log文件无法一行行读取出来

[复制链接]
发表于 2012-6-14 08:36:35 | 显示全部楼层 |阅读模式
本帖最后由 boyhong 于 2012-6-14 09:41 编辑

一个log文件无法一行行读取出来,我是用如下代码来读行。。

If FileExists(@ScriptDir&"\faultykeyboard.log") Then
Local $file = FileOpen(@ScriptDir&"\faultykeyboard.log",0)
If $file <> -1 Then
; 每次读取一行文本,直到文件结束.
For $i=1 To 10
    Local $line = FileReadLine($file,$i)
    If @error = -1 Then ExitLoop
    MsgBox(0, "第"&$i&"行:", $line)
Next
FileClose($file)
EndIf
EndIf


为什么呢?我抱着试验的态度更改fileopen后的参数,看是否Unicode的原因,但不怎么懂,希望大家帮我分析一下,若能讲解一下就更是我等菜鸟之福了:
测试文件下载:http://pan.baidu.com/netdisk/singlepublic?fid=377820_692661576
发表于 2012-6-14 08:59:25 | 显示全部楼层
Local $sFile = @ScriptDir & '\faultykeyboard.log'
If Not FileExists($sFile) Then Exit -1
Local $hfile = FileOpen($sFile, 32), $iL = 1, $line
While 1
        $line = FileReadLine($hfile)
        If @error = -1 Then ExitLoop
        MsgBox(0, '第' & $iL & '行:', $line)
        $iL += 1
WEnd
FileClose($hfile)
发表于 2012-6-14 09:06:15 | 显示全部楼层
如果需要创建数组 则用正则更方便
Local $sFile = @ScriptDir & '\faultykeyboard.log'
If Not FileExists($sFile) Then Exit -1
Local $hfile = FileOpen($sFile, 32)
Local $str = FileRead($hfile)
FileClose($hfile)
Local $aSR = StringRegExp($str, '\V+', 3), $iL
If @Error Then Exit -2
For $iL = 0 To UBound($aSR) - 1
        MsgBox(0, '第' & $iL + 1 & '行:', $aSR[$iL])
Next

评分

参与人数 1金钱 +10 贡献 +1 收起 理由
boyhong + 10 + 1 感谢A版。

查看全部评分

发表于 2012-6-14 09:10:27 | 显示全部楼层
回复 1# boyhong
也可以这样读, 如果懒得多打一两行代码:
#include <file.au3>
Local $a
_FileReadToArray(@ScriptDir & '\faultykeyboard.log', $a)
If @error Then Exit MsgBox(16, 'Err', '读取文件内容失败!')
For $i = 1 To $a[0]
        MsgBox(0, '文件内容', '第 ' & $i & ' 行: ' & @CRLF & $a[$i])
Next

评分

参与人数 1金钱 +10 贡献 +1 收起 理由
boyhong + 10 + 1 感谢您的热心帮助。

查看全部评分

发表于 2012-6-14 09:46:32 | 显示全部楼层
回复  boyhong
也可以这样读, 如果懒得多打一两行代码:
user3000 发表于 2012-6-14 09:10



    这个文件没有 BOM 标识,默认会以 ANSI 格式读取到错误的数据,所以这样是不行的。

评分

参与人数 1金钱 +20 贡献 +2 收起 理由
user3000 + 20 + 2 谢谢! 没实际测试真不应该发言啊!

查看全部评分

您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-30 07:31 , Processed in 0.078630 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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