StreamReader reads an extra 1,024 bytes...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lord.zoltar@gmail.com

    #1

    StreamReader reads an extra 1,024 bytes...

    Hi, I'm having some trouble with a StreamReader.
    I use a System.IO.Strea mReader to read a text file and then print it.
    If the user has selected a range of pages starting past the first page,
    I have to make the StreamReader jump to some point in the file.
    To make the StreamReader jump to where it should be, I use the
    following code:

    fReader.BaseStr eam.Seek(fileBo okmark + (Me.reportTextW idth *
    linesPerPage * startPage), IO.SeekOrigin.B egin)

    I've run this through the debugger, and the expression "fileBookma rk +
    (Me.reportTextW idth * linesPerPage * startPage)" seems to be
    calculating the starting character correctly.
    When I start reading the file, I use:

    line = fReader.ReadLin e()

    ....and this is where the problems occur!! I set a watch in the
    debugger on fReader.BaseStr eam.Position. After the first operation (the
    seek, shown above), fReader.BaseStr eam.Position is 2479. After the
    ReadLine executes, fReader.BaseStr eam.Position is 3503, it has
    increased by 1,024 Bytes! For all subsequent ReadLines,
    fReader.BaseStr eam.Position does not change at all. Maybe I'm just not
    quite understanding how the VB.NET StreamReader works... Does anyone
    have an idea why this would be happening? Or maybe a better way to do
    what I'm trying to do:
    Jump to a position in a text file and read!

  • Peter Ritchie [MVP]

    #2
    RE: StreamReader reads an extra 1,024 bytes...

    StreamReader is buffering the reads. When you ask it to read 1 byte it goes
    ahead and gets 1024 to improve performance. The next byte you ask for
    doesn't actually require a read operation, it just grabs the byte from the
    buffer it's using.

    Is this a problem, as in are you not getting the data you expect?

    --

    Microsoft MVP, Visual Developer - Visual C#


    "lord.zoltar@gm ail.com" wrote:
    Hi, I'm having some trouble with a StreamReader.
    I use a System.IO.Strea mReader to read a text file and then print it.
    If the user has selected a range of pages starting past the first page,
    I have to make the StreamReader jump to some point in the file.
    To make the StreamReader jump to where it should be, I use the
    following code:
    >
    fReader.BaseStr eam.Seek(fileBo okmark + (Me.reportTextW idth *
    linesPerPage * startPage), IO.SeekOrigin.B egin)
    >
    I've run this through the debugger, and the expression "fileBookma rk +
    (Me.reportTextW idth * linesPerPage * startPage)" seems to be
    calculating the starting character correctly.
    When I start reading the file, I use:
    >
    line = fReader.ReadLin e()
    >
    ....and this is where the problems occur!! I set a watch in the
    debugger on fReader.BaseStr eam.Position. After the first operation (the
    seek, shown above), fReader.BaseStr eam.Position is 2479. After the
    ReadLine executes, fReader.BaseStr eam.Position is 3503, it has
    increased by 1,024 Bytes! For all subsequent ReadLines,
    fReader.BaseStr eam.Position does not change at all. Maybe I'm just not
    quite understanding how the VB.NET StreamReader works... Does anyone
    have an idea why this would be happening? Or maybe a better way to do
    what I'm trying to do:
    Jump to a position in a text file and read!
    >
    >

    Comment

    • lord.zoltar@gmail.com

      #3
      Re: StreamReader reads an extra 1,024 bytes...


      Peter wrote:
      StreamReader is buffering the reads. When you ask it to read 1 byte it goes
      ahead and gets 1024 to improve performance. The next byte you ask for
      doesn't actually require a read operation, it just grabs the byte from the
      buffer it's using.
      >
      Is this a problem, as in are you not getting the data you expect?
      >
      Exactly. Actually, it's weird: the page started printing at character
      2353. I want it to start reading at EXACTLY 2479.

      Comment

      • Peter Ritchie [MVP]

        #4
        Re: StreamReader reads an extra 1,024 bytes...

        How are you creating your StreamReader object, through a c'tor and a filename
        (getting the default FileStream object for the BaseStream property), or are
        you using a constructor that takes a Stream object?

        Seek/ReadLine should work fine, buffered IO should only read in new data
        (and affecting the Position property) when it needs too.

        You haven't specified what the actual problem is, other than Position isn't
        changing as you expected (but as it should, regardless of what you expect).
        If you're using ReadLine, how do you know what value to use for Seek?

        You'll have to use something other than FileStream if you want unbuffered IO.

        --

        Microsoft MVP, Visual Developer - Visual C#


        "lord.zoltar@gm ail.com" wrote:
        >
        Peter wrote:
        StreamReader is buffering the reads. When you ask it to read 1 byte it goes
        ahead and gets 1024 to improve performance. The next byte you ask for
        doesn't actually require a read operation, it just grabs the byte from the
        buffer it's using.

        Is this a problem, as in are you not getting the data you expect?
        Exactly. Actually, it's weird: the page started printing at character
        2353. I want it to start reading at EXACTLY 2479.
        >
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: StreamReader reads an extra 1,024 bytes...

          <lord.zoltar@gm ail.comwrote:
          Peter wrote:
          StreamReader is buffering the reads. When you ask it to read 1 byte it goes
          ahead and gets 1024 to improve performance. The next byte you ask for
          doesn't actually require a read operation, it just grabs the byte from the
          buffer it's using.

          Is this a problem, as in are you not getting the data you expect?
          Exactly. Actually, it's weird: the page started printing at character
          2353. I want it to start reading at EXACTLY 2479.
          What character encoding are you using? If you're using any character
          encoding which doesn't use a fixed number of bytes per character, you
          basically can't skip to a desired character without reading the rest.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • lord.zoltar@gmail.com

            #6
            Re: StreamReader reads an extra 1,024 bytes...


            Exactly. Actually, it's weird: the page started printing at character
            2353. I want it to start reading at EXACTLY 2479.
            >
            What character encoding are you using? If you're using any character
            encoding which doesn't use a fixed number of bytes per character, you
            basically can't skip to a desired character without reading the rest.
            >
            Err, it's whatever the default is. The file is a plain text ASCII file,
            as far as I can tell. The same program that reads it also writes it,
            and in all my text editors, it looks like plain ASCII text.

            Comment

            • lord.zoltar@gmail.com

              #7
              Re: StreamReader reads an extra 1,024 bytes...

              How are you creating your StreamReader object, through a c'tor and a filename
              (getting the default FileStream object for the BaseStream property), or are
              you using a constructor that takes a Stream object?
              I'm doin' it like this:
              fReader = New System.IO.Strea mReader(Me.file Name)
              Seek/ReadLine should work fine, buffered IO should only read in new data
              (and affecting the Position property) when it needs too.
              >
              You haven't specified what the actual problem is, other than Position isn't
              changing as you expected (but as it should, regardless of what you expect).
              If you're using ReadLine, how do you know what value to use for Seek?
              The exact problem is that I want to start reading (and then printing)
              the file at character #2479, and it started at character #2353.
              I calculate the position to seek to based on the starting page the user
              requested in the print dialogue, the number of lines that will be
              printed on a page, and the number of characters printed across the page
              (yes it's ugly and awful and I was asked to do text reports that look
              like this... /rolls eyes ;) ).
              The specific code for the seeking looks like this:
              fReader.BaseStr eam.Seek(fileBo okmark + (Me.reportTextW idth *
              linesPerPage * startPage), IO.SeekOrigin.B egin)
              You'll have to use something other than FileStream if you want unbuffered IO.
              >
              I might be able to get rid of the text reports and use a proper
              reporting system, in which case this whole problem may soon become
              academic...:)
              but it's not there yet.

              What I thought I would try tomorrow would be to replace the ReadLine
              with a combination of ReadBlock (since I will know exactly how many
              characters per line) and Seek.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: StreamReader reads an extra 1,024 bytes...

                <lord.zoltar@gm ail.comwrote:
                Exactly. Actually, it's weird: the page started printing at character
                2353. I want it to start reading at EXACTLY 2479.
                What character encoding are you using? If you're using any character
                encoding which doesn't use a fixed number of bytes per character, you
                basically can't skip to a desired character without reading the rest.
                >
                Err, it's whatever the default is. The file is a plain text ASCII file,
                as far as I can tell. The same program that reads it also writes it,
                and in all my text editors, it looks like plain ASCII text.
                Okay, in that case you should be okay if you set the stream's position
                (the Position property is easier to use than Seek btw - at least in my
                view) and then call StreamReader.Di scardBufferedDa ta().

                Could you post a short but complete program which demonstrates the
                problem?

                See http://www.pobox.com/~skeet/csharp/complete.html for details of
                what I mean by that.

                --
                Jon Skeet - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: StreamReader reads an extra 1,024 bytes...

                  Lord,

                  Are you sure there is no null byte in the string that you are reading?

                  Cor

                  <lord.zoltar@gm ail.comschreef in bericht
                  news:1153752639 .318643.47310@m 79g2000cwm.goog legroups.com...
                  Hi, I'm having some trouble with a StreamReader.
                  I use a System.IO.Strea mReader to read a text file and then print it.
                  If the user has selected a range of pages starting past the first page,
                  I have to make the StreamReader jump to some point in the file.
                  To make the StreamReader jump to where it should be, I use the
                  following code:
                  >
                  fReader.BaseStr eam.Seek(fileBo okmark + (Me.reportTextW idth *
                  linesPerPage * startPage), IO.SeekOrigin.B egin)
                  >
                  I've run this through the debugger, and the expression "fileBookma rk +
                  (Me.reportTextW idth * linesPerPage * startPage)" seems to be
                  calculating the starting character correctly.
                  When I start reading the file, I use:
                  >
                  line = fReader.ReadLin e()
                  >
                  ...and this is where the problems occur!! I set a watch in the
                  debugger on fReader.BaseStr eam.Position. After the first operation (the
                  seek, shown above), fReader.BaseStr eam.Position is 2479. After the
                  ReadLine executes, fReader.BaseStr eam.Position is 3503, it has
                  increased by 1,024 Bytes! For all subsequent ReadLines,
                  fReader.BaseStr eam.Position does not change at all. Maybe I'm just not
                  quite understanding how the VB.NET StreamReader works... Does anyone
                  have an idea why this would be happening? Or maybe a better way to do
                  what I'm trying to do:
                  Jump to a position in a text file and read!
                  >

                  Comment

                  • lord.zoltar@gmail.com

                    #10
                    Re: StreamReader reads an extra 1,024 bytes...


                    Cor Ligthert [MVP] wrote:
                    Lord,
                    >
                    Are you sure there is no null byte in the string that you are reading?
                    >
                    Cor
                    >
                    Sorry in not responding for so long, the printing section suddenly
                    became low-priority as other stuff came up. It still will have to be
                    done so I will still have to try all the suggestions here, but maybe
                    not as soon as I had liked :(

                    Cor, I was taking newline characters into account, 1 for each line.
                    Where would a nullbyte be? I would have only expected it at the end of
                    the file.

                    Comment

                    Working...