BinaryReader in Byte[]

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello!

I would like insert my data(who i get on my binaryreader) in an array byte.
nd after, i would like modify it and save it with my BinaryWriter this is my
code.
My file is always empty.

Thank's for your help,

Wavemill

FileStream myStream = new FileStream(@"\MyFile.bmp", FileMode.Open);
BinaryReader myReader = new BinaryReader(myStream);

FileStream myStreamWriter = new FileStream(@"\MyFile.bmp", FileMode.Create);
BinaryWriter myWriter = new BinaryWriter(myStreamWriter);

for(int i=0;i<myStreamWriter.Length;i++)
{
myBitmapByte = myReader.ReadByte();
}

//here i would like modify my array byte

I write my file
foreach (byte myByte in myBitmapByte)
{
myWriter.Write(myByte);
}
myWriter.Flush();

myStream.Close();
myReader.Close();
myStreamWriter.Close();
myWriter.Close();
 
My file is always empty.
[...]
FileStream myStream = new FileStream(@"\MyFile.bmp", FileMode.Open);
BinaryReader myReader = new BinaryReader(myStream);

FileStream myStreamWriter = new FileStream(@"\MyFile.bmp", FileMode.Create);
BinaryWriter myWriter = new BinaryWriter(myStreamWriter);

Since you specify FileMode.Create when opening the second FileStream,
the original file will be replaced before you start reading from it.
One solution would be to write to another temporary file and not
replace the existing one until you're done.

for(int i=0;i<myStreamWriter.Length;i++)
{
myBitmapByte = myReader.ReadByte();
}


reading and writing byte by byte seems a bit inefficient. You can read
it all at once with FileStream.Read or BinaryReader.ReadBytes.


Mattias
 
Thank you for your reply.

FileStream myStream = new FileStream(@"\MyFile.bmp", FileMode.Open);
BinaryReader myReader = new BinaryReader(myStream);

FileStream myStreamWriter = new FileStream(@"\MyFile.bmp", FileMode.Create);
BinaryWriter myWriter = new BinaryWriter(myStreamWriter);

Sorry, but when i have written the code i make mistake, MyFile.bmp in reder
and writer is diferrent.

I would like recover my reader in byte[], modify it and after writing a new
file.
When i try this with my code my file is empty.

Have you got an idea?

wavemill
 
Back
Top