dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: StreamReader.PeekLine

  1. #1
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Question 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

  2. #2
    Join Date
    Oct 2009
    Posts
    6

    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.

  3. #3
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    Re: StreamReader.PeekLine

    What do you mean?

    I thought the BaseStream.Position will change after I called ReadLine method.

  4. #4
    Join Date
    Oct 2009
    Posts
    6

    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.

  5. #5
    Join Date
    Sep 2005
    Location
    Cracow, Poland
    Posts
    345

    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

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,330

    Re: StreamReader.PeekLine

    Quote Originally Posted by yoyosh View Post
    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.

  7. #7
    Join Date
    May 2007
    Posts
    1,546

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)