Re: StreamReader.PeekLine
Code:
public string PeekLine()
{
while (true)
{
long peekPos = this.BaseStream.Position;
string line = this.ReadLine();
this.BaseStream.Position = peekPos;
return line;
}
}
You are just re-assigning
Code:
this.BaseStream.Position = peekPos;
to the the same thing it was. I think you are trying to take in a number and then assign it to peekPos?
If so you need to assign it to line.
Re: StreamReader.PeekLine
What do you mean?
I thought the BaseStream.Position will change after I called ReadLine method.
Re: StreamReader.PeekLine
You are not assigning
Code:
string line = this.ReadLine();
to anything but line.
I think you are trying to do this?
Code:
this.BaseStream.Position = (int) line;
To change the position number?
Quote:
I thought the BaseStream.Position will change after I called ReadLine method.
It will if you assign it to something that you are going to use.
All you are doing is taking in a number and assigning it to line.
Then returning line.
If you are trying to take a number in and setting it to your posistion you need to assign it to the position.
Re: StreamReader.PeekLine
No, this part is correct in my view. Im not assigning line anywhere but I`m calling method ReadLine which should modify position. The problem is that this stream is buffered. Take a look at:
http://social.msdn.microsoft.com/For...a-83b471cd4cf9
Re: StreamReader.PeekLine
Quote:
Originally Posted by
yoyosh
I created a class deriving from StreamReader and I added the following method to it:
public string PeekLine()
{
while (true)
{
long peekPos = this.BaseStream.Position;
string line = this.ReadLine();
this.BaseStream.Position = peekPos;
return line;
}
}
However it does not work.
this.BaseStream.Position equals to 1024 before and after 'this.ReadLine()'. Where did I make a mistake?
Thank you for help in advance
Given the code that you have here if it does not equal the previous value it will because you are explicitly telling it to be.
In other words you are geting the position before the read and then after the read you are setting the position to what it was before the read so what is it that is not working?
Re: StreamReader.PeekLine
With the different character encodings out there (utf8, utf16, ascii) you cannot reliably seek forward and backward in the stream once you have surrogate characters. Bear that in mind before you try to implement a generic seeking solution. You've already gotten full solutions in your other thread so there's no point in explaining anything new here ;)