我现在要对一个文本进行操作,从第一行开始读。每读一行就进行相关的操作,然后在读下一行,依此类推,只到读完全部文本。谢谢各位最好能给点代码参考。

解决方案 »

  1.   

    Dim nFile as integer, strTmp as string
        nFile = FreeFile
        Open 全路径文件名 For Input As #nFile
        Do While Not EOF(nFile)
            Line Input #nFile, strTmp 'strtmp为读取的一行字符
            ......
        Loop
        
        Close #nFile
      

  2.   

    先谢谢上面的了,代码可以用。但是这样的话,所有读出行的操作都相同了,我每读一行操作是不同的。也就是说我读第一行时是写入到文件1里面,我读第二行时是写入到文件2里面。
    能不能有一种方面像是操作数据库那样的,就像MoveNext那样直接移到下一条记录。
      

  3.   

    '定义一计数器来处理不就完了?!
    dim nCounter as Long ' 计数器
    Dim nFile as integer, strTmp as string
        nFile = FreeFile
        Open 全路径文件名 For Input As #nFile
        Do While Not EOF(nFile)
            Line Input #nFile, strTmp 'strtmp为读取的一行字符
            nCounter=nCounter+1   'nCounter即第几行
             
            ......
        Loop
        
        Close #nFile
      

  4.   

    你意思是是判断这个计数器nCounter的数来执行相关操作吧。
    就是用 
    if nCounter = 1 then
       相关操作....
    end ifif nCounter = 2 then
       相关操作....
    end if这样的话那不是如果文本有多少行那就要定义多少个if或switch case?如果能让文本直接操完成一条记录的相关操作后,自动移到下一条记录在进行相关操作,是不是会更好一些?
      

  5.   

    Line Input本身就是读取一行后自动移到下一行啦。
    你每行的处理不同,当然要根据是哪一行判断处理,跟自动移行什么关系?
      

  6.   

    Dim arrb() as byte,s() as string,i as long
     Open "c:\xxx.txt" For Binary As #1 
         ReDim arrb(LOF(1)) As Byte 
         Get #1, , arrb 
         Close 1 
    erase arrb
    s()=split(strconv(arrb,vbunicode),vbcrlf)
    for i=0 to ubound(s)
    call 相关操作(i)
    next
    erase s
      

  7.   

    northwolves(狼行天下)的处理是
    1、将文件读入到一字节数组中(Get #1, , arrb )
    2、将数组转为字符串(strconv(arrb,vbunicode))
    3、根据回车换行符(vbcrlf))来分解该字符串到一数组s()(s()=split(strconv(arrb,vbunicode),vbcrlf)
    )
    4、数组s()每个元素就是文本每行的字符串
    5、对数组s循环,对每行进行操作 call 相关操作(i)
       其中这个 相关操作(i)仅限于操作与行有规律的情况
      

  8.   

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' GetLyrics
    ' Purpose: 
    '    Displays the contents of the lyrics files.
    ' Demonstrates the following 
    '  - FileSystemObject.OpenTextFile
    '  - FileSystemObject.GetFile
    '  - TextStream.ReadAll
    '  - TextStream.Close
    '  - File.OpenAsTextStream
    '  - TextStream.AtEndOfStream
    '  - TextStream.ReadLine
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Function GetLyrics(FSO)   Dim TextStream
       Dim S
       Dim File   ' There are several ways to open a text file, and several 
       ' ways to read the data out of a file. Here's two ways 
       ' to do each:   Set TextStream = FSO.OpenTextFile(TestFilePath & "\Beatles\OctopusGarden.txt", OpenFileForReading)
       
       S = TextStream.ReadAll & NewLine & NewLine
       TextStream.Close   Set File = FSO.GetFile(TestFilePath & "\Beatles\BathroomWindow.txt")
       Set TextStream = File.OpenAsTextStream(OpenFileForReading)
       Do    While Not TextStream.AtEndOfStream
          S = S & TextStream.ReadLine & NewLine
       Loop
       TextStream.Close   GetLyrics = S
       
    End Function