For Programmers: Free Programming Magazines  


Home > Archive > C# > May 2005 > 2 Questions on Reading Binary File; "Conversion buffer overflow"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author 2 Questions on Reading Binary File; "Conversion buffer overflow"
poifull

2005-05-25, 3:59 am

Hi All,

What is the proper way to read a binary file into a byte[]?
I am using BinaryReader to read from a Stream and call the ReadByte method
of the BinaryReader object.

The method I'm using leads to the second question. I got the "Conversion
buffer overflow" error when I run the following code:
Stream s = openFileDialog1.OpenFile();

System.IO.BinaryReader br = new BinaryReader(s);

byte[] ba = new byte[s.Length]; // s.Length ~= 20000

while (br.PChar() > -1) {

ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when error
occurred

}

Thanks in advance




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

2005-05-25, 8:57 pm

Hi,


Take a look at this code:
int readed=0;
byte[] buff = new Byte[2048];
FileStream fstream = new FileStream( filename, FileMode.Open);

while( (readed=fstream.Read( buff, 0, 2048))>0 )
networkstream.Write( buff, 0, readed);
fstream.Close();


Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"poifull" <poifull76@yahoo.com> wrote in message
news:kITke.128225$h6.79530@tornado.texas.rr.com...
> Hi All,
>
> What is the proper way to read a binary file into a byte[]?
> I am using BinaryReader to read from a Stream and call the ReadByte method
> of the BinaryReader object.
>
> The method I'm using leads to the second question. I got the "Conversion
> buffer overflow" error when I run the following code:
> Stream s = openFileDialog1.OpenFile();
>
> System.IO.BinaryReader br = new BinaryReader(s);
>
> byte[] ba = new byte[s.Length]; // s.Length ~= 20000
>
> while (br.PChar() > -1) {
>
> ba[s.Position] = br.ReadByte(); // <- s.Position ~= 400 when
> error occurred
>
> }
>
> Thanks in advance
>
>
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com