- public void WritingABinaryBun() ///Writing
- {
- System.IO.FileStream wFile;
- wFile = new FileStream(Path.Combine(OuputsPath, "SC03Brick201.bun"), FileMode.Create);
- byte[] bytes = BitConverter.GetBytes(-1);
- if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
- wFile.Write(bytes, 0, bytes.Length);
- bytes = BitConverter.GetBytes(10);
- if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
- wFile.Write(bytes, 0, bytes.Length);
- bytes = BitConverter.GetBytes(164);
- if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
- wFile.Write(bytes, 0, bytes.Length);
- }
- public void ReadingABinaryBun() //Reading
- {
- using (BinaryReader b = new BinaryReader(File.Open(Path.Combine(InputsPath, "SC03Brick201.bun"), FileMode.Open)))
- {
- int pos = 0;
- int length = (int)b.BaseStream.Length;
- //long length1 = (long)b.BaseStream.Length;
- while (pos < length)
- {
- int v = b.ReadInt32();
- //double v1 = b.ReadDouble();
- pos += sizeof(int);
- }
- }
- }
Writing and then reading a binary- inconsistency/incorrect
Below are 2 code snippets (which are a concise form of my code) which I'm using for writing and reading a binary file.
As you see, I have written 3 integer values into a binary file.
But when I read the same file through the second code snippet, I see only the foirst integer value of -1 is correctly read in.
Can anybody please help em understand why is it so?
I am using C#
Midhun TpPosted Sep 1, 2016, 11:18 AM
Sahil DevPosted Sep 1, 2016, 12:12 PM
Midhun TpPosted Sep 1, 2016, 11:29 AM
Sahil DevPosted Sep 1, 2016, 11:25 AM
yes, it does, thanks a lot.
I'm not sure whythe old developer has put that. Any idea?
Sahil DevPosted Sep 1, 2016, 11:05 AM
Yes, I know. BinaryWriter gives correct values.
But there is a lot of existing code whichis as you have in my first snippet.
Midhun TpPosted Sep 1, 2016, 10:56 AM