Finding a StreamReader's true position

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Keith Kingsley

    Finding a StreamReader's true position

    I'm using a StreamReader to read in several lines from an ASCII file. I'd
    like to know the StreamReader's "true" position-- that is, the number of
    bytes into the file that the StreamReader has read. I thought about using
    MyStreamReader. BaseStream.Posi tion, but this always seems to return a
    multiple of the StreamReader's buffer size (which seems natural-- as I
    understand it the StreamReader reads from the underlying stream in discrete
    blocks corresponding to the buffer size). I also considered counting the
    bytes myself (as I read into the file), but ReadLine() doesn't record whether
    the returned line ended in a CR, an LR, or both.

    Any ideas?

    Thanks,
    Keith Kingsley
    --
    What if there were no hypothetical situations?
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Finding a StreamReader's true position

    Hi,

    You can't using Streamreader for the same reason you expose. the solution
    may be using a stream that does gives you the Position and then decorate it
    with streamreader to have the "line" functionality.

    Use a FileStream to open the file and them create a streamreader around it.
    you could use FileStream.Posi tion to know the position.

    Even so, this may not be the solution, as the FileStream.Posi tion will give
    you what streamreader has readed, not what you consume.
    Try it, if it does not work. you have to implement it . I think I have such
    a code, I used it for a PPC application to read from a networkstream and
    have a ReadString () like method. let me know if you need the code


    cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation





    "Keith Kingsley" <KeithKingsley@ discussions.mic rosoft.com> wrote in message
    news:7F811708-E073-4293-A2E6-E967BC4913A7@mi crosoft.com...[color=blue]
    > I'm using a StreamReader to read in several lines from an ASCII file. I'd
    > like to know the StreamReader's "true" position-- that is, the number of
    > bytes into the file that the StreamReader has read. I thought about using
    > MyStreamReader. BaseStream.Posi tion, but this always seems to return a
    > multiple of the StreamReader's buffer size (which seems natural-- as I
    > understand it the StreamReader reads from the underlying stream in
    > discrete
    > blocks corresponding to the buffer size). I also considered counting the
    > bytes myself (as I read into the file), but ReadLine() doesn't record
    > whether
    > the returned line ended in a CR, an LR, or both.
    >
    > Any ideas?
    >
    > Thanks,
    > Keith Kingsley
    > --
    > What if there were no hypothetical situations?[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Finding a StreamReader's true position ( CODE )

      Here is the code


      public string ReadStringFromN etwork( )
      {
      StringBuilder buff;
      try
      {
      buff = new StringBuilder( 20);
      int ch;
      while( (ch=networkstre am.ReadByte())! = -1)
      {
      if (ch == 13) continue;
      if ( ch==10) return buff.ToString() ;
      buff.Append( Convert.ToChar( ch));

      }
      }
      catch(Exception e)
      {
      throw new Exception("\n== >Method: NetAccess.ReadS tringFromNetwor k,
      reading string :" + e.Message );

      }
      return buff.ToString() ;
      }


      PLEASE NOTE:
      the code above assume that each character is 1 byte long, this is not true
      with unicode so you have to be careful with this.


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation




      Comment

      Working...