mexiaoyuoo 发表于 2008-9-11 00:55:05

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

比如a.txt
内容
测试1
测试2
...
就是.txt文件每行有几个字
现在就是不知道如何才能随机取某一行的内容,并输入在记事本上面!
麻烦高手帮小弟弟想想办法!

mexiaoyuoo 发表于 2008-9-11 00:57:05

郁闷,上次注册的帐号忘记密码。取回时56邮箱已经停用。郁闷死了。上面的问题大哥哥们帮我解决一下吧。我想用来选学生的名字!:face (36):

pcbar 发表于 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)))

mexiaoyuoo 发表于 2008-9-11 12:22:39

:face (32): 弹出窗口空白标题为0

pcbar 发表于 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的函数库有点问题,不能正确计算含有中文的文件行数

wxlj 发表于 2008-10-4 16:31:17

:face (25): :face (25): :face (25): :face (25): :face (25):

liongodmien 发表于 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 编辑 ]

sanhen 发表于 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
EndFunc   ;==>__FileCountLines

KEYFree2008 发表于 2008-11-16 20:54:42

3楼及5楼的方法在 V3.2.13.8.1 中能正确识别中文了

[ 本帖最后由 KEYFree2008 于 2008-11-16 21:32 编辑 ]

sky 发表于 2009-10-10 10:52:38

用了5楼的方法。不错。!!

顶贴留名,日后可用。

lyb0618 发表于 2009-10-10 11:42:54

我也留名备用

foolbyrd 发表于 2009-10-15 12:26:55

这个有用,谢谢~~备用

sxd 发表于 2009-10-15 13:29:43

新版已经修复这个bug

bing614 发表于 2009-10-15 13:34:18

揍下热闹#Include <File.au3>
$_Paht=@SystemDir & "\eula.txt"
Dim $aRecords
If _FileReadToArray($_Paht,$aRecords) Then
        $Random=Random(1,$aRecords,1)
        MsgBox(0, $Random & " 行",$aRecords[$Random])
Else
        MsgBox(0,"","读取失败!")
        Exit
EndIf

sxd 发表于 2009-10-15 13:42:02

我觉得楼上的代码水平远远超过了语文水平
:face (33):
页: [1] 2
查看完整版本: 高手解决下,如何随机取.txt某行值