Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Read and Write Structures to Files with .NET

0.00/5 (No votes)
10 Aug 2005 1  
Reading and writing structure data to and from Binary files
Sample Image - readwritestructstobinfile.jpg

Introduction

I have always found that when transporting data via files, one of the best methods is to store that data into a structure and then read it out in that structure again. Two of the main reasons for this would be: No one can read the data out the files without the structure and if you are transporting that data via TCP/IP, the traffic is reduced as the files are "compressed".

You could even use a binary file as a very simple data store when you have a small task to perform and do not want to transport your application with a database.

Description

The source file is basically one class which you can plug and play into any project.

The Constructor

The constructor of the class accepts the file name and a type. This is the struct type. i.e.: If you had a struct:

public struct MyStruct 
{ 
    public int i; 
    public string sz; 
} 

Then the type passed into the constructor would be the type of the struct, i.e.:

StructFile structfile = new StructFile(@"c:\test.dat", typeof(MyStruct)); 
 
... 
 
public StructFile(string szFile, System.Type type) 
{ 
    _File = szFile; 
    _oType = type; 
} 

To use the class after it has been initialized, you need to call Open to open the files. This is a wrapper for the FileStream object and accepts the same parameters.

As the class had to be generic enough to cater to all structures, it is internally an object type.

Writing the Struct to the File

public bool WriteStructure(object oStruct) 
{ 
    _oStruct = oStruct; 
    try 
    { 
        byte[] buf = StructToByteArray(); 
        BinaryWriter bw = new BinaryWriter(_fs); 
        bw.Write(buf); 
        bw.Close(); 
        bw = null; 
        return true; 
    } 
    catch (Exception ex) 
    { 
        throw ex; 
    } 
}

private byte[] StructToByteArray() 
{ 
    try 
    { 
        // This function copies the structure data into a byte[] 

        //Set the buffer to the correct size 
        byte[] buffer = new byte[Marshal.SizeOf(_oStruct)]; 

        //Allocate the buffer to memory and pin it so that GC cannot use the 
        //space (Disable GC) 
        GCHandle h = GCHandle.Alloc(buffer , GCHandleType.Pinned); 

        // copy the struct into int byte[] mem alloc 
        Marshal.StructureToPtr(_oStruct, h.AddrOfPinnedObject(), false); 

        h.Free(); //Allow GC to do its job 

        return buffer; // return the byte[]. After all that's why we are here 
                       // right. 
    } 
    catch (Exception ex) 
    { 
        throw ex; 
    } 
} 

The structure is passed into the WriteStructure function as an object. The object is then converted to a byte[] which binarywriter can use to print the data.

Reading the Data Back

public object GetNextStructureValue() 
{ 
    byte[] buffer = new byte [Marshal.SizeOf(_oType)]; 

    object oReturn = null; 
    try 
    { 
        if (EOF) return null; 
        _fs.Read(buffer, 0, buffer.Length); 
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); 
        oReturn = (object)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),
                                                _oType); 
        handle.Free(); 
        if (_fs.Position >= _fs.Length) 
            Close(); 
        return oReturn; 
    } 
    catch (Exception ex) 
    { 
        throw ex; 
    } 
} 

Always remember to close the file once you are finished with it. :)

History

  • 10th August, 2005: Initial post

License

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