Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C#
Article

Read and Write Structures to Files with .NET

Rate me:
Please Sign up or sign in to vote.
3.66/5 (14 votes)
10 Aug 2005CPOL1 min read 139.4K   6.2K   22   31
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:

C#
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.:

C#
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

C#
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

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Spain Spain
Born 1981
Finished School in 1999
Developer ever since

Happy coding
All things are possible through Christ who strengthens me.

Comments and Discussions

 
GeneralRe: Terrible idea Pin
Joe Woodbury11-Aug-05 9:54
professionalJoe Woodbury11-Aug-05 9:54 
GeneralRe: Terrible idea Pin
Jamie Nordmeyer11-Aug-05 10:05
Jamie Nordmeyer11-Aug-05 10:05 
GeneralRe: Terrible idea Pin
Joe Woodbury11-Aug-05 10:15
professionalJoe Woodbury11-Aug-05 10:15 
GeneralRe: Terrible idea Pin
Jamie Nordmeyer11-Aug-05 10:16
Jamie Nordmeyer11-Aug-05 10:16 
GeneralRe: Terrible idea Pin
Todd Beaulieu30-Oct-06 6:11
Todd Beaulieu30-Oct-06 6:11 
GeneralRe: Terrible idea Pin
Joe Woodbury30-Oct-06 6:32
professionalJoe Woodbury30-Oct-06 6:32 
GeneralRe: Terrible idea Pin
seejay112023-Mar-10 6:36
seejay112023-Mar-10 6:36 
GeneralWhat's the difference-- Pin
dzCepheus11-Aug-05 7:32
dzCepheus11-Aug-05 7:32 
--between this and using the BinarySerializer? Does this provide some performance gain over the BinarySerializer?

PS: This is what part of the alphabet would look like if the letters Q and R were removed.
GeneralGreat work Pin
Thomas Lykke Petersen10-Aug-05 20:47
Thomas Lykke Petersen10-Aug-05 20:47 
GeneralRe: Great work Pin
kornman0012-Aug-05 12:50
kornman0012-Aug-05 12:50 
GeneralA much easier way Pin
leppie10-Aug-05 7:12
leppie10-Aug-05 7:12 
GeneralRe: A much easier way Pin
HumanOsc11-Aug-05 3:49
HumanOsc11-Aug-05 3:49 

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.