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

怎样得到文本文件中有多少行?

[复制链接]
发表于 2009-4-24 05:48:56 | 显示全部楼层 |阅读模式
本帖最后由 huangque 于 2009-4-25 06:17 编辑

求教:
如何得知一个文本文件中有多少行?
发表于 2009-4-24 06:22:10 | 显示全部楼层
$aArray = StringSplit(FileRead($sFile), @CRLF, 1)
$aArray[0] 就是你要的东东

详解:先用FileRead读入整个文件,再用StringSplit将读入变量按换行回车符分解为数组,数组单元0代表有效内容总数,也就是文件行数。
发表于 2009-4-24 08:51:31 | 显示全部楼层
学习了,非常有用,下来试试看!
发表于 2009-4-24 13:45:47 | 显示全部楼层
#Include<File.au3>
_FileCountLines($filePath)
发表于 2009-4-24 15:40:07 | 显示全部楼层
_Main()

Func _Main()
        Local $File
        Local $sum
        $File = @ScriptDir & "\test.txt" ;<--你要读的文件
        $sum = _CountLines($File)
        If $sum = -1 Then
                MsgBox(0, "错误", "不能打开文件")
        Else
                MsgBox(0, "读取完毕", $File & " 共" & $sum & "行")
        EndIf
        Exit
EndFunc   ;==>_Main

Func _CountLines($File)
        Local $m = 0, $t
        Local $FileHandle
        $FileHandle = FileOpen($File, 0)
        If $FileHandle = -1 Then
                Return -1
        Else
                While 1
                        $t = FileReadLine($FileHandle)
                        If @error = -1 Then
                                ExitLoop
                        Else
                                $m = $m + 1
                        EndIf
                WEnd
        EndIf
        FileClose($FileHandle)
        Return $m
EndFunc   ;==>_CountLines
 楼主| 发表于 2009-4-25 06:16:40 | 显示全部楼层
感谢!问题解决。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-4-28 20:56 , Processed in 0.074623 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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