Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Want To Read My Binary File Which Has Char , String , Integers In It . So How Should I Read It And I Have Tried To Read My File My With Below Function And That Work's Fine But If I Want To Read The Binary File Were I Don't Know What Is The Next Typeof Data Type . So How To Read A Binary File Were I Don't What Is The Next Available Data Type .

C#
static void read() // Read Function 
    {
        FileStream a = new FileStream("bin.txt", FileMode.Open);
        BinaryReader b = new BinaryReader(a);

        int pos = 0;
        Console.WriteLine(b.ReadChar());
        Console.WriteLine(b.ReadChar());
        Console.WriteLine(b.ReadString());
        Console.WriteLine(b.ReadInt32());
        }

static void write() // Write Function This What My File consist Of Data. 
    {
        BinaryWriter al = new BinaryWriter(File.Create("bin.txt"));
        char a = 'l';
        al.Write(a);
       a = 'p';
        al.Write(a);
        string l = "loremm";
        al.Write(l);
        al.Write(1233);
        al.Close();
    }


Sorry In Case Of Any TYPO
Posted
Comments
BillWoodruff 19-Dec-15 4:11am    
If you did not know me, and I sent you a letter composed of words encrypted with my own software then saved as a byte[] ... and I sent it to you in an envelope with no return address ... would you know who it was from, or what it meant ?
Hitesh Jain 19-Dec-15 5:54am    
I Can't Get What You Are Saying
BillWoodruff 19-Dec-15 10:52am    
"I Can't Get What You Are Saying"

That is exactly the point, here. What you do not know the structure of, or have a schema for: you will not get !

First, to read any data you must know the structure of that data before hand or the structure must be embedded with-in the data.

Second, for string type data always add a length deliminator before the actual string value in the data stream since it will be of variable length (preferably also know the code page format before hand like ASCII, UTF8 etc.)
 
Share this answer
 
You can't store store data randomly in a binary file - you have to know exactly what is in there, either by "fixing" the structure and reading it back in that format - so your code knows what to read as it wrote it basically - or by including "markers" in the binary data which tell you what is coming.
If you make a mistake, it can really mess up your data!
I'd suggest this if you don't know what you are doing, then look at using binary serialization and de-serialisation instead of direct binary access, as that handles it for you.
This may help: Object Serialization using C#[^]
 
Share this answer
 
This is how any binary serialization works: before any piece of data, you write some descriptor of this data. In particular, you have to write some word which uniquely denotes the data type of next record. It can be anything, but it should definitively identify what's written next. Of course, the reading and writing part of your code should use the same definitions. It resembles some transport protocol. Before each string, it's lengths should be written. Before the collection of object, the length of this collection should be written. And so on…

Now, it will only be a fully-fledged serialization, if you make it type-agnostic. This is possible to achieve through reflection, and it's not trivial at all. You can probably understand it yourself if you understand how serialization works. Please see:
http://en.wikipedia.org/wiki/Serialization[^],
https://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

No, MSDN won't give you explanation on the internals, but if you want to develop such thing, you should be able to imagine it and design all the functionality. Or… perhaps you can use available serializer:
https://msdn.microsoft.com/en-us/library/72hyey7b%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28v=vs.110%29.aspx[^].

Unfortunately, this serializer does not use data contract.

Now, the final and perhaps the hardest step: as reflection is slow, there is a way to accelerate it, at least not to perform all reflection work over and over on the same data model. This is achieved by System.Serialization.Emit, which emits the serialization code on the fly. Such code requires fine understanding of CIL and is quite hard to debug. Care to try? :-)

See also:
https://en.wikipedia.org/wiki/Common_Intermediate_Language[^],
https://msdn.microsoft.com/en-us/library/system.reflection.emit%28v=vs.110%29.aspx[^],
http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570[^],
Dynamic Type Using Reflection.Emit[^].

—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