Click here to Skip to main content
Licence CPOL
First Posted 10 Aug 2005
Views 56,680
Downloads 1,846
Bookmarked 18 times

Read and Write Structures to Files with .NET

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

About the Author

zadeveloper.com

Architect
Unitrade
South Africa South Africa

Member

Born Jarrod Pereira in 1981
 
Finished School in 1999
 
Developer ever since Smile | :)
 
Happy coding.
 
All things are possible through Christ who strengthens me.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCrashes when using arrays Pinmemberflyboyqw14:52 7 Nov '11  
Questiongreat work, but a clarification needed Pinmemberrmsohaila10:26 6 Nov '11  
GeneralAttempted to read or write protected memory. This is often an indication that other memory is corrupt. PinmemberReza_m_n_651:08 3 Jun '11  
GeneralRe: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. PinmemberMember 42356139:00 5 Sep '11  
QuestionError when read Struct with string Element? Pinmemberhuynhdangthai17:09 18 May '10  
QuestionHow to correctly write strings within a struct PinmemberTony Zackin11:03 12 Feb '10  
GeneralWonderful! Excellent job! PinmemberGraham Downs4:21 5 Jan '10  
GeneralRe: Wonderful! Excellent job! PinmemberJarrodPereira5:22 5 Jan '10  
GeneralBinarySerializer Pinmemberdung16:20 18 Jun '08  
GeneralAttempted to read or write protected memory. This is often an indication that other memory is corrupt. Pinmemberdimas099:28 7 Mar '08  
QuestionHow t o write next record in a text file using the same code Pinmembersajeee4:28 18 Nov '07  
AnswerRe: How t o write next record in a text file using the same code PinmemberJarrodPereira8:11 18 Nov '07  
GeneralAutomate Write Type Pinmemberjasonbullard20:37 21 Jul '07  
Questionan interesting problem Pinmemberteyteyi4:14 1 Jun '07  
Questionhow to write structers in vb.net Pinmemberladdu270:42 6 May '06  
GeneralDon't work with string Pinmemberleodez12:20 28 Feb '06  
GeneralTerrible idea PinmemberJoe Woodbury8:12 11 Aug '05  
GeneralRe: Terrible idea PinmemberJamie Nordmeyer9:19 11 Aug '05  
GeneralRe: Terrible idea PinmemberJoe Woodbury9:54 11 Aug '05  
GeneralRe: Terrible idea PinmemberJamie Nordmeyer10:05 11 Aug '05  
GeneralRe: Terrible idea PinmemberJoe Woodbury10:15 11 Aug '05  
GeneralRe: Terrible idea PinmemberJamie Nordmeyer10:16 11 Aug '05  
GeneralRe: Terrible idea PinmemberTodd Beaulieu6:11 30 Oct '06  
GeneralRe: Terrible idea PinmemberJoe Woodbury6:32 30 Oct '06  
GeneralRe: Terrible idea Pinmemberseejay11206:36 23 Mar '10  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Aug 2005
Article Copyright 2005 by zadeveloper.com
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid