retrieve a specific line in text file

K

kevin arana

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.
 
H

Herfried K. Wagner [MVP]

kevin arana said:
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?

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

kevin arana

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.
 
H

Herfried K. Wagner [MVP]

kevin arana said:
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.


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.
 
C

Cor Ligthert [MVP]

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
 
C

Chris Dunaway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top