D
Danny Smith
Hi,
I need to read a file and be able to:
1. Find the current position in the stream
2. Have access to a handy ReadLine() method.
Obviously the FileStream class supports random access, so you have a Seek()
method and a Position property to find the current stream position, and the
StreamReader class has a ReadLine() method. I thought using these together
would give me what I needed:
StreamReader sr = new StreamReader( File.OpenRead( filename ) );
string line = sr.ReadLine();
long position = sr.BaseStream.Position;
However, this does not work as the StreamReader has buffering which means
the moment a Read call is made, much more than the single line is read from
the BaseStream. Consequently the Position property does not return the
position *immediately* after the end of the line, which is what I need.
This is possible in Java with the RandomAccessFile class, which supports
methods for reading lines and seeking / obtaining the position. Is there
are way to do this in C#?
One option would be to write my own ReadLine method which would allow me to
keep track of the stream position, but I'd like to know if there is a way to
avoid this.
Can anyone help?
Danny Smith
I need to read a file and be able to:
1. Find the current position in the stream
2. Have access to a handy ReadLine() method.
Obviously the FileStream class supports random access, so you have a Seek()
method and a Position property to find the current stream position, and the
StreamReader class has a ReadLine() method. I thought using these together
would give me what I needed:
StreamReader sr = new StreamReader( File.OpenRead( filename ) );
string line = sr.ReadLine();
long position = sr.BaseStream.Position;
However, this does not work as the StreamReader has buffering which means
the moment a Read call is made, much more than the single line is read from
the BaseStream. Consequently the Position property does not return the
position *immediately* after the end of the line, which is what I need.
This is possible in Java with the RandomAccessFile class, which supports
methods for reading lines and seeking / obtaining the position. Is there
are way to do this in C#?
One option would be to write my own ReadLine method which would allow me to
keep track of the stream position, but I'd like to know if there is a way to
avoid this.
Can anyone help?
Danny Smith