473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StreamReader reads an extra 1,024 bytes...

Hi, I'm having some trouble with a StreamReader.
I use a System.IO.StreamReader 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.BaseStream.Seek(fileBookmark + (Me.reportTextWidth *
linesPerPage * startPage), IO.SeekOrigin.Begin)

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

line = fReader.ReadLine()

....and this is where the problems occur!! I set a watch in the
debugger on fReader.BaseStream.Position. After the first operation (the
seek, shown above), fReader.BaseStream.Position is 2479. After the
ReadLine executes, fReader.BaseStream.Position is 3503, it has
increased by 1,024 Bytes! For all subsequent ReadLines,
fReader.BaseStream.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!

Jul 24 '06 #1
9 5063
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?

--
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"lo*********@gmail.com" wrote:
Hi, I'm having some trouble with a StreamReader.
I use a System.IO.StreamReader 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.BaseStream.Seek(fileBookmark + (Me.reportTextWidth *
linesPerPage * startPage), IO.SeekOrigin.Begin)

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

line = fReader.ReadLine()

....and this is where the problems occur!! I set a watch in the
debugger on fReader.BaseStream.Position. After the first operation (the
seek, shown above), fReader.BaseStream.Position is 2479. After the
ReadLine executes, fReader.BaseStream.Position is 3503, it has
increased by 1,024 Bytes! For all subsequent ReadLines,
fReader.BaseStream.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!

Jul 24 '06 #2

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.

Jul 24 '06 #3
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.

--
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"lo*********@gmail.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.

Jul 24 '06 #4
<lo*********@gmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 24 '06 #5

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.

Jul 24 '06 #6
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.StreamReader(Me.fileName)
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.BaseStream.Seek(fileBookmark + (Me.reportTextWidth *
linesPerPage * startPage), IO.SeekOrigin.Begin)
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.

Jul 24 '06 #7
<lo*********@gmail.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.DiscardBufferedData().

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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 24 '06 #8
Lord,

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

Cor

<lo*********@gmail.comschreef in bericht
news:11*********************@m79g2000cwm.googlegro ups.com...
Hi, I'm having some trouble with a StreamReader.
I use a System.IO.StreamReader 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.BaseStream.Seek(fileBookmark + (Me.reportTextWidth *
linesPerPage * startPage), IO.SeekOrigin.Begin)

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

line = fReader.ReadLine()

...and this is where the problems occur!! I set a watch in the
debugger on fReader.BaseStream.Position. After the first operation (the
seek, shown above), fReader.BaseStream.Position is 2479. After the
ReadLine executes, fReader.BaseStream.Position is 3503, it has
increased by 1,024 Bytes! For all subsequent ReadLines,
fReader.BaseStream.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!

Jul 25 '06 #9

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.

Aug 2 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keith Kingsley | last post by:
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...
4
by: martin | last post by:
Hi, I am copying an xml file like so. Dim xmlDoc As New XmlDocument xmlDoc.Load("C:\Program Files\Templates\message.msg") Console.WriteLine("Tmaplate loaded") xmlDoc.Save("C:\Program...
2
by: cagey cretin | last post by:
I have set up a catch/try deal to find an error, but there are none reported nor when I clean the build/rebuild. Simple filecalled text_file.txt. I cannot write to it, but I can write into it with...
1
by: martinaguilar | last post by:
I have 2 text files. No matter what I do, this code always read lines from the same file and not from the file specified in txtFile.Text. I can't get it... any idea why? Dim oRead As...
14
by: karthikbalaguru | last post by:
Hi, In the case of heap , to keep track of a single chunk of memory it requires 8 bytes of information. That is, it requires 4 bytes to hold the size, and 4 bytes to hold the pointer to the next...
4
by: Rymfax | last post by:
Greetings, I have an app that uses HttpWebRequest to download a rather large file (100 MB). I have a while loop set up so that it reads 4096 bytes at a time from the ResponseStream() until it...
1
by: ShapeMan | last post by:
I'm trying to export data to a binary file. I have imported the data from a binary file using BinaryReader and everything works as I would have expected. The data after importing is a variety of...
4
by: Fuzz13 | last post by:
I'm writing a program that asks a user to navigate to a file (.txt or .csv) load that file into memory then streamreader reads through the file to end and every time it hits a carriage return "\n" or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.