找回密码
 加入
搜索
查看: 5933|回复: 19

高手解决下,如何随机取.txt某行值

[复制链接]
发表于 2008-9-11 00:55:05 | 显示全部楼层 |阅读模式
比如a.txt
内容
测试1
测试2
...
就是.txt文件每行有几个字
现在就是不知道如何才能随机取某一行的内容,并输入在记事本上面!
麻烦高手帮小弟弟想想办法!
 楼主| 发表于 2008-9-11 00:57:05 | 显示全部楼层
郁闷,上次注册的帐号忘记密码。取回时56邮箱已经停用。郁闷死了。上面的问题大哥哥们帮我解决一下吧。我想用来选学生的名字!
发表于 2008-9-11 08:41:02 | 显示全部楼层
#include "file.au3"
$line=_FileCountLines('c:\a.txt')
MsgBox(0,0,FileReadLine('c:\a.txt',Random(1,$line,1)))
 楼主| 发表于 2008-9-11 12:22:39 | 显示全部楼层
弹出窗口空白标题为0
发表于 2008-9-11 15:25:39 | 显示全部楼层
#include "acn_file.au3"
$line=_FileCountLines('c:\a.txt')
MsgBox(0,0,FileReadLine('c:\a.txt',Random(1,$line,1)))

file.au3的函数库有点问题,不能正确计算含有中文的文件行数
发表于 2008-10-4 16:31:17 | 显示全部楼层
发表于 2008-10-4 17:50:55 | 显示全部楼层
容易的不能用,就用土办法!


For $i = 1 To 65536
        FileReadLine("C:\a.txt", $i)
        If @error Then ExitLoop
        $i = $i + 1
Next
$i = $i - 1
While 1
        $Lines = Random(1, $i, 1)
        MsgBox(64, "TEST", "读取了第" & $Lines & "行" & @CRLF & "内容为:" & @CRLF & FileReadLine("C:\a.txt", $Lines), 2)
Wend


[ 本帖最后由 liongodmien 于 2008-10-4 17:54 编辑 ]
发表于 2008-10-4 18:14:57 | 显示全部楼层
acn_file.au3函数不知道为什么没有修正。
以前收藏的以下两个函数都可以正确返回含有中文的文本文件的行数,有兴趣测试一下。


$line=_FileCountLinesEx('1.txt')
MsgBox(0,0,FileReadLine('1.txt',Random(1,$line,1)))



Func _FileCountLinesEx($sFilePath)
    Local $iCountLines = 0
    Local $sFRead = FileRead($sFilePath)
    
    If @error = -1 Then Return SetError(1, 0, 0) 
    If @error <> 0 Then Return SetError(2, 0, 0) 
    
    $sFRead = StringReplace($sFRead, @CRLF, "")
    $iCountLines += @extended
    
    $sFRead = StringReplace($sFRead, @CR, "")
    $iCountLines += @extended
    
    StringReplace($sFRead, @LF, "")
    $iCountLines += @extended
    
    Return $iCountLines + 1
EndFunc

Func _FileCountLinesX($sFilePath)
    Local $hFile, $sFileContent, $aTmp
    $hFile = FileOpen($sFilePath, 0)
    If $hFile = -1 Then Return SetError(1, 0, 0)
    $sFileContent = FileRead($hFile, FileGetSize($sFilePath))
    FileClose($hFile)
    If StringInStr($sFileContent, @LF) Then
        $aTmp = StringSplit(StringStripCR($sFileContent), @LF)
    ElseIf StringInStr($sFileContent, @CR) Then
        $aTmp = StringSplit($sFileContent, @CR)
    Else
        If StringLen($sFileContent) > 0 Then Return 1
        Return SetError(2, 0, 0)
    EndIf
    Return $aTmp[0]
EndFunc   ;==>__FileCountLines

发表于 2008-11-16 20:54:42 | 显示全部楼层
3楼及5楼的方法在 V3.2.13.8.1 中能正确识别中文了

[ 本帖最后由 KEYFree2008 于 2008-11-16 21:32 编辑 ]
发表于 2009-10-10 10:52:38 | 显示全部楼层
用了5楼的方法。不错。!!

顶贴留名,日后可用。
发表于 2009-10-10 11:42:54 | 显示全部楼层
我也留名备用
发表于 2009-10-15 12:26:55 | 显示全部楼层
这个有用,谢谢~~备用
发表于 2009-10-15 13:29:43 | 显示全部楼层
新版已经修复这个bug
发表于 2009-10-15 13:34:18 | 显示全部楼层
揍下热闹
#Include <File.au3>
$_Paht=@SystemDir & "\eula.txt"
Dim $aRecords
If _FileReadToArray($_Paht,$aRecords) Then
        $Random=Random(1,$aRecords[0],1)
        MsgBox(0, $Random & " 行",$aRecords[$Random])
Else
        MsgBox(0,"","读取失败!")
        Exit
EndIf
发表于 2009-10-15 13:42:02 | 显示全部楼层
我觉得楼上的代码水平远远超过了语文水平
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-9-22 18:29 , Processed in 0.080789 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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