473,386 Members | 1,943 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,386 software developers and data experts.

Reading a file byte by byte

Hello everyone,

I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?

Here is my code:

/// <summary>
/// Opens a file chooser.
/// </summary>
///
private void openToolStripMenuItem_Click(object sender,
EventArgs e)
{
openFileDialog.ShowDialog();

string fileName = openFileDialog.FileName;

FileStream fileReader = new FileStream(fileName,
FileMode.Open, FileAccess.Read);
StringBuilder buffer = new StringBuilder();

long length = fileReader.Length;
long position = 0;

while (position < length) // is this correct ?? or should
it be length - 1 instead of length ?
{
position++;
buffer.Append(fileReader.ReadByte());
}
textBox.Text = buffer.ToString();

}
}

May 7 '07 #1
6 14655
Seabass <sh****@upei.cawrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Yes - read it in blocks instead of reading a byte at a time.

Note that it's also a bad idea to save the results in a StringBuilder.
That's for *strings*, not binary data. MemoryStream is, in some ways,
the binary equivalent of StringBuilder.

I have a class in my miscutil library that helps to read the whole of a
stream in one go. See
http://pobox.com/~skeet/csharp/miscutil

Note also that you should use a "using" statement so that your stream
is always closed. (In fact, you're *never* closing it in your code.)

--
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
May 7 '07 #2
Hi,

"Seabass" <sh****@upei.cawrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
Hello everyone,

I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?

Well, what do you expect? of course it will takes forever.

A better approach is to read a chunk of the file and then process it from
memory.

Also beware with big files, using StringBuilder may not be the best way for
this.

I would just read what the screen can show, in this way you do not read
content that will not be visible in the screen
May 7 '07 #3
Seabass wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.

If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.

Arne
May 12 '07 #4
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:
Seabass wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?

Others have already suggested reading many bytes instead
of one byte at a time.

If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.

Arne
Hi there,

Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams.

http://msdn2.microsoft.com/en-us/lib...redstream.aspx

Hope this helps.

Moty

May 13 '07 #5
Moty Michaely wrote:
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:
>Seabass wrote:
>>I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.

If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.
Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams.
That was already in what you quoted ...

Arne
May 19 '07 #6
On May 20, 2:05 am, Arne Vajhøj <a...@vajhoej.dkwrote:
Moty Michaely wrote:
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.dkwrote:
Seabass wrote:
I'm trying to do a Hex Viewer in C#. I'm reading the user selected
file byte by byte but it seems to take forever ( one minute to read a
larger file ). Is there any way to make this function work faster ?
Others have already suggested reading many bytes instead
of one byte at a time.
If you really like ReadByte you can just wrap your Stream
in a BufferedStream, that should get rid of the worst ineffciencies
of using ReadByte.
Check the BufferedStream class that adds a buffering layer to read and
write operations of other streams.

That was already in what you quoted ...

Arne
Yep, Sorry for that, I missed that :)

Moty

May 20 '07 #7

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

Similar topics

14
by: Job Lot | last post by:
I have tab delimited text file which gets populated on daily basis via automated process. New entry is written at the bottom. I need to create a utility which makes a copy of this file with 10 most...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
3
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows...
4
by: dale zhang | last post by:
Hi, I am trying to save and read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp Right now, I saved images without any...
10
by: T Cordon | last post by:
I am using a StreamReader to read text from an HTML file and display it as part of a page in a Label Control. Buy it is not displaying characters as: ñ, ó, ú, etc. Please Help. Thanks
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
8
by: junk5 | last post by:
Hi I need to read raw 16 bit data from a file, where the first byte is the most significant byte of the first data value and the second byte is the least significant byte of the first data value...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
6
by: Stephen | last post by:
I am reading a text file using TextReader reader = new StreamReader("file.txt"); like I have done many a times before. I then execute the statement string a = reader.ReadToEnd(); this string is...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.