|
-
May 25th, 2010, 10:55 AM
#1
StreamReader.PeekLine
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
-
May 25th, 2010, 11:13 AM
#2
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.
-
May 25th, 2010, 11:38 AM
#3
Re: StreamReader.PeekLine
What do you mean?
I thought the BaseStream.Position will change after I called ReadLine method.
-
May 25th, 2010, 12:03 PM
#4
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?
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.
-
May 25th, 2010, 12:22 PM
#5
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
-
May 25th, 2010, 12:49 PM
#6
Re: StreamReader.PeekLine
 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?
Last edited by DataMiser; May 25th, 2010 at 12:53 PM.
Always use [code][/code] tags when posting code.
-
May 25th, 2010, 06:04 PM
#7
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
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|