retrieve a specific line in text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kevin arana

    #1

    retrieve a specific line in text file

    Hello.

    Could anyone please show me an example of how to retrieve a specific line of
    a text file (say line 3 of data.txt) into a string variable?

    I have seen many examples on how to dump all the content into a string, and
    how to read line-by-line consecutively, but how can i read just 1 specific
    line?

    Thank you.


  • Herfried K. Wagner [MVP]

    #2
    Re: retrieve a specific line in text file

    "kevin arana" <none> schrieb:[color=blue]
    > Could anyone please show me an example of how to retrieve a specific line
    > of a text file (say line 3 of data.txt) into a string variable?[/color]

    Read the lines and skip the data read from the file until you reach line n.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • kevin arana

      #3
      Re: retrieve a specific line in text file

      Yes but this isnt exactly what i am looking for.

      I saw an example posted in one of these newsgroups a long time a go and lost
      the snippet. It worked really well.

      If you could help me reproduce that example, i would apreciate it.


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: retrieve a specific line in text file

        "kevin arana" <none> schrieb:[color=blue]
        > Yes but this isnt exactly what i am looking for.
        >
        > I saw an example posted in one of these newsgroups a long time a go and
        > lost the snippet. It worked really well.[/color]


        I feel sorry, but I don't think that 'StreamReader' provides a way to read a
        certain line without reading the preceeding lines. There is nowhere stored
        in the file where line n begins.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: retrieve a specific line in text file

          Kevin,

          As Herfried wrote in a text file, you can only do as Herfried wrote.

          In additon trying his message more clear you have only to read until that
          the 3th line is read, than you can stop.

          I hope this helps,

          Cor


          Comment

          • Chris Dunaway

            #6
            Re: retrieve a specific line in text file

            If the lines are all the same length, then you might be able to use the
            Seek method of the stream to seek to the line you want and then read
            that one line.

            Comment

            Working...