Reading a file byte by byte

S

Seabass

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();



}


}
 
J

Jon Skeet [C# MVP]

Seabass said:
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.)
 
I

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

Hi,

Seabass said:
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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Seabass said:
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
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top