PDA

Click to See Complete Forum and Search --> : Binary File Data / Textbox


Barguast
Aug 19th, 2004, 10:27 AM
I'm new to C#, and the first program that I'm doing is a clone of Notepad. This basically consists of a TextBox control docked on a form.

All was going well until I tried to open something other than a text file (namely a bitmap). The problem is that certain characters ('\0' for example) won't display, and worse still won't allow any other characters to appear after them.

I think this is a limitation of the TextBox control, but I'm not sure. This is how I'm reading the file data:

BinaryReader file = new BinaryReader (File.Open (dlgOpenFile.FileName, FileMode.Open, FileAccess.Read));

byte [] buffer = new byte [1024];

while (file.BaseStream.Position != file.BaseStream.Length)
{
int bytesRead = file.Read (buffer, 0, 1024);
string text = System.Text.Encoding.ASCII.GetString (buffer, 0, bytesRead);
txtText.Text += text;
}

file.Close ();

This seems to read the file data properly, but when it is added to the TextBox (txtText) object, it all seems to go wrong.

So, am I reading the file properly? Is the textbox control capable of displaying these characters? Does anyone know where I'm going wrong? :D

Thanks.

PT Exorcist
Aug 30th, 2004, 03:35 AM
Open it with File.OpenRead() instead

sunburnt
Aug 30th, 2004, 02:10 PM
No, the textbox can't display a null character -- think about how strings of text are handled in memory: a character array, followed by a null character to indicate the end of the string. When the textbox encouters a null character, it interprets that as the end of the displayable text. If you want to display binary data, you should display it in hexidecimal format or similar way.

Good luck!