Click here to Skip to main content
12,695,447 members (26,772 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked

Intelligent Binary File Reader

, 25 Jul 2005
Rate this:
Please Sign up or sign in to vote.
Reads binary records. If the record structure changes, you don't need to change the source code.


Many a times we need to read records from binary files. While reading a record, it is necessary to know the structure of the record. Once we know the structure of the record, it becomes simple to read from the binary file.

But what happens if the structure changes? What happens if the field type in the structure changes? Do we need to write the source code again?

This is what I will try to do in this article. Read a binary file without coding the structure of field type.

Input file

I created a simple binary file with the following record in it:

CUSTOMER_ID       1 
ORDER_QTY         100 
RATE              10.00

Good old XML CONFIG file

Once we know how the input file looks like, we can re-create the structure as some input to our reader. I decided to use XML. You can use anything that has the flexibility to read all types of data; we also put some Reader method info in the XML. In all the XML looks like this:

 <method_info type="int">ReadInt32</method_info>
 <method_info type="double">ReadDouble</method_info>
 <method_info type="string">ReadString</method_info>
 <method_info type="bool">ReadBoolean</method_info>

The method_info tag is used to define the type of the field. You can use any name int, double, string, bool as I have used, or Tom, Dick, and Harry. What is important here is to put the right method to read a particular type. Here, I have put the methods of the class BinaryReader.

The node record contains the order of fields. For now, I have this for the field_type property. This can be easily extended to field_name, default values, valid value ranges etc.

public class IBIOReader

First, let us start discussing about the fields:

// fields
protected Hashtable method_info_table = 
                             new Hashtable();
protected ArrayList record = new ArrayList();
protected BinaryReader brdr;
protected bool init = false;

The method_info_table contains the key value pair of the type in method_info tag and MethodInfo object of System.Reflection namespace. The MethodInfo object contains the BinaryReader's Read method to invoke.

The record list contains the field_type of the record tag.

Public methods

  • public void Initialize(string i_xml_path)

    The parameter i_xml_path gives the path and name of the file from which to load the reader configuration. Initialize method populates the record list and the method_info_table.

    In order to populate the method_info_table, we use the System.Reflection.MethodInfo class. This is accomplished in the following way:

    Type binReaderType = 
    MethodInfo mi = binReaderType.GetMethod(method);
    public bool Open(string file_name)

    This opens the binary file with the name file_name. Returns false, if the Reader is not initialized.

  • public int ReadNextRecord(ref ArrayList r_record)

    The method used to read a particular type is picked up from the method_info_table. The Invoke method of the MethodInfo object is then called. In code form ...

    MethodInfo mi = (MethodInfo)method_info_table[s];
    object obj = mi.Invoke(brdr, null);

    reads the next record. r_record contains the list of objects in the record. The function returns 0 if end of stream is reached, -1 if there is an error and 1 if the read is successful.

  • public bool Open(string file_name)

    Open a file with the name file_name. Returns false, if the reader is not initialized.

  • public void Close()

    Close the reader.

    In all the methods, exception need to be caught at the previous level.

How to Use?

Here is the sample source code to use this reader:

using IntelliBinIO;
 ArrayList record = new ArrayList();
 IBIOReader rdr = new IBIOReader();
 // initialize
 // open file
 // read each record
 while (rdr.ReadNextRecord(ref record) != 0)
  Console.WriteLine("Read Record Done! Values are:-");
  foreach (object obj in record)
 // close file
catch(Exception e)

Improvements and enhancements

  1. Allow customized readers.
  2. Instead of returning an ArrayList of objects, create a class called Field, which has properties like name, value, valid values, type etc.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Jitesh Patil
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralArticle Needs Update Pin
coloncm28-Sep-07 10:34
membercoloncm28-Sep-07 10:34 
GeneralAfter Update Pin
Bothe16-Jul-07 1:32
memberBothe16-Jul-07 1:32 
QuestionWhy are you guys rating this article 1? Pin
mav.northwind26-Jul-05 21:23
membermav.northwind26-Jul-05 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 25 Jul 2005
Article Copyright 2005 by Jitesh Patil
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid