none
How to read a perticular line from a text file ? not reading line by line RRS feed

  • Question

  • Hi,

    I have approximately 300 MB text file, I want to change one particular line, I know exactly which line number the changes are  to be made. 

    How can achieve this quickly without reading the file line by line and then reaching to that line to do something.

     

    Could you please help.

     

    regards,

    Avesh

     

    Monday, January 23, 2012 2:58 PM

All replies

  • "How can achieve this quickly without reading the file line by line "

    You need to know - exactly - the length of every line, in which case you might be able to take advantage of the file structure. Otherwise, you need to read line by line.... and write it back out to the changed file.

    If your file is properly structured, you can use random access:

    http://www.erlandsendata.no/english/index.php?d=envbafileaccessrandom


    HTH, Bernie

    Monday, January 23, 2012 3:26 PM
  • If your each line is separated by a vbCrLf then you can use this code as well.

     

    Sub Sample()
        Dim MyData As String, strData() As String
        
        '~~> Read the entire text file in one GO
        Open "C\Sample.Txt" For Binary As #1
        MyData = Space$(LOF(1))
        Get #1, , MyData
        Close #1
        strData() = Split(MyData, vbCrLf)
        
        '~~> Read the 300th line
        MsgBox strData(299)
    End Sub
    


    Sid (A good exercise for the Heart is to bend down and help another up) Please do not email me your questions. I do not answer questions by email unless I get paid for it :) If you want, create a thread in VB.Net/Excel forum and email me the link and I will help you if I can.
    Monday, January 23, 2012 6:21 PM
    Moderator
  • Siddharth,

    Thank you very much for your response but it did not work. In MyData returned empty.

     

    regards,

    Avesh
    Monday, January 23, 2012 11:23 PM
  • 1) Are the line separated by vbCrLf?

    2) If yes, then which line are you trying to get? and what value have you set for X

    MsgBox strData(X)

    Sid (A good exercise for the Heart is to bend down and help another up) Please do not email me your questions. I do not answer questions by email unless I get paid for it :) If you want, create a thread in VB.Net/Excel forum and email me the link and I will help you if I can.
    Monday, January 23, 2012 11:35 PM
    Moderator
  • Hi Avesh,

    How about the problem on your side? Do you still need any assistant about the problem?

    Did you tried Siddharth suggestion?

    If you still show any concern on the problem, just feel free to let us know.
     
    Best Regards,


    Daniel van den Berg | Washington, USA | "Anticipate the difficult by managing the easy"

    Wednesday, February 15, 2012 8:22 PM
    Moderator
  • 300 MB text file... OMG

    Import this to some database.


    Oskar Shon, Office System MVP

    Press if Helpful; Answer when a problem solved

    Thursday, February 16, 2012 1:07 PM
    Answerer