473,586 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 openToolStripMe nuItem_Click(ob ject sender,
EventArgs e)
{
openFileDialog. ShowDialog();

string fileName = openFileDialog. FileName;

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

long length = fileReader.Leng th;
long position = 0;

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

}
}

May 7 '07 #1
6 14667
Seabass <sh****@upei.ca 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 ?
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.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
May 7 '07 #2
Hi,

"Seabass" <sh****@upei.ca wrote in message
news:11******** **************@ h2g2000hsg.goog legroups.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.d kwrote:
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.d kwrote:
>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.d kwrote:
Moty Michaely wrote:
On May 13, 1:33 am, Arne Vajhøj <a...@vajhoej.d kwrote:
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
4491
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 recent entries. When I read line using StreamReader object it starts from the top, so looping though lines and keeping track of the line is not...
3
9500
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I...
3
3625
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 application, while I am doing for web. I can only find Image from web forms control and HTML control. This may be the root cause of my problem. For...
4
3283
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 errors. After reading the ole object from db, I saved it to C: as file1.bmp and displayed on the web. But it can not be displayed. After I manually sent...
10
1686
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
1839
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 (an endless loop). i thought that BinaryReader.ReadByte advanced to the next byte? i had it time out after 1000 iterations, and keeps outputting...
8
7244
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 (the next pair or bytes and the next etc. specify the second and third values). For example, if I had a file containing 0xff22
11
3577
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
16
3735
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 two questions.. 1- why if i change fread(bmp1, sizeof(bmp1), 1, fin); to fread(bmp1, sizeof(struct bmp), 1, fin); i have a Segment violation ??
6
4005
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 then sent into a function to convert its contents (hex string) into a byte array. Well when this pass occurs the HextoByteArray function says the input...
0
7911
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.