reading data from ascii text file

x86

Member
Joined
Jan 22, 2006
Messages
10
Programming Experience
Beginner
Hello, i'm trying to read a ascii text file using the streamreader.
For simplicity, let's say my .txt file contains the following data:
a
b
c
d
e
f
g
How would i read from, let's say character c to f?
In pseudocode, i would be something like:
for starting charcter = "c" to 3

It would read 3 lines after c, up to f, and give me:
c
d
e
f

I would appreciate your help!
 
When reading a file using a StreamReader you ALWAYS have to start from the beginning. You can simply discard any lines you don't want. As for how you would do what you ask, it depends on excatly what you want. Do you want to read from the first occurrence of "c" to the first occurrence of "f" or do you want to read from the third to the seventh line, regardless of what they contain? The algorithm you would use depends on the general case.
 
That's a very good point. I want it to read from the first occurence of let's say "c" and 3 lines after that. That's it.
 
Then I'd suggest that you use two loops. The first one would call ReadLine until it found the string you want, discarding all the previous lines. The second would read the next three lines. Remember that you may reach the end of the file at any point before acheiveing your goal so you'd need to test for that. You could do it all in one loop if you wanted but it may be clearer if you use two.
 
Back
Top