Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Can anyone help me in the foll. issue:
I need to read data in local variables from a binary file generated sequentially in linux. It is very much difficult for me to read the int's, float's and double values from the binary file. As they generate separate outputs when read as stream and read as sequential.

What I have tried so far:
I have tried BinaryFormatter to convert the binary file to readable stream, but it throws an exception of "The input stream is not a valid binary format. The starting contents (in bytes) are: 26-41-6B-73-68-61-79-20-20-20-20-20-20-20-20-20-20 ..."

I even tried BinaryReader, but only able to read strings/chars, but the numbers come different and have no relation the type declared.

The StreamReader is no good in this case.. This is my assumption as StreamReader will read a stream and not sequentially as I want.

Please help.. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 25-Nov-13 2:04am    
"Even"?! You must have screwed up something. This is way too typical: blaming some classes for your own lack of skills.
—SA
Akshay 007 25-Nov-13 2:47am    
Hi Sergey,

To make things clear I am not using BinaryWriter to create the binary file. As said earlier the file is created on linux with another tool. The method of access is used as sequential for both reading and writing. My task here involves that I need to read that binary file in C# and use these values for further tasks and write the output to another binary file.


The length of my binary file is 40 as per br.BaseStream.Length, the string that is present in the binary file is "Akshay" and was initialized for 30 characters space. The float value inserted is "1000.50" and the int value inserted is "1234567890". All the 3 values are written in same line with tab delimited. When I read it using BinaryReader and as you mentioned about reading the values, I used ReadChars(30) for reading the string of 30 characters, then I used ReadSingle() as an alternative for float as both represent 4 byte floating value, and laslty I used ReadInt32(() as ReadInt64() will go beyond the stream length.

Foll is my code for reading
<pre>
using (var br = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 0x10000, FileOptions.SequentialScan)))
{
long pos, length;
length = br.BaseStream.Length;
pos = br.BaseStream.Position;

char[] name = br.ReadChars(30);
pos = br.BaseStream.Position;

float fVal = br.ReadSingle();
pos = br.BaseStream.Position;

Int64 intVal = br.ReadInt64();
pos = br.BaseStream.Position;


}
</pre>
Please help me in understanding where I am making the mistake.
Sergey Alexandrovich Kryukov 25-Nov-13 3:07am    
So what? I already answered. Want to write as well? Use BinaryWriter, in a way symmetrical to reading, method-by-method.
Look, do you have fixed format? Can you change it? If you can, just write read and write at the same time, side by side.
Now, why binary? Maybe, if it seems difficult for you (but why?), you could use XML? Them you won't need to read/write anything directly, not at all; you could use DataContract... Or use serialization with BinaryFormatter.
—SA
Akshay 007 25-Nov-13 3:50am    
There are some applications that are made keeping Linux in mind, and these applications use binary format as their input. I don't think you are concentrating on the part where I say while writing the binary file the access mode set is sequential. It is similar to the access="SEQUENTIAL" in Fortran.
I will be using Binary Writer to write the output of my tasks, but it has to be in the same format as is the reader.
Sergey Alexandrovich Kryukov 25-Nov-13 3:58am    
I don't have to "concentrate" on anything anymore, just because I already have you exact recipe of reading any binary file, no matter what it is. I only added some alternative just in case. What else is unclear?
—SA

"All the 3 values are written in same line with tab delimited"
And where do you read the tabs? Nowhere. After every br.ReadSomething() call you must advance the position by an additional byte. A br.ReadByte() could help. - Do not do that after the last read operation.
And just calculate:
30 (for the string) + 1 (for the first tab) + 4 (for the float) + 1 (for the second tab) + 4 (for the int) = 40 bytes.
 
Share this answer
 
You really need to know all the detail of the file format. Reading is quite simple:
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readdouble(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.readsingle(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^].

If you make a tiny mistake in an offset of each numeric or any other field, you start to read between fields and screw up everything after such shift. So, you need to be accurate.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900