Wednesday, March 30, 2011

Read an array of structs in C#

Hi,

I've seen here , and also googling for "marshal" several ways to convert a byte array to a struct.

But what I'm looking for is if there is a way to read an array of structs from a file (ok, whatever memory input) in one step?

I mean, load an array of structs from file normally takes more CPU time (a read per field using a BinaryReader) than IO time. Is there any workaround?

I'm trying to load about 400K structs from a file as fast as possible.

Thanks

pablo

From stackoverflow
  • Following URL may be of interest to you.

    http://www.codeproject.com/KB/files/fastbinaryfileinput.aspx

    Or otherwise I think of pseudo code like the following:

    readbinarydata in a single shot and convert back to structure..

    public struct YourStruct {

    public int First;

    public long Second;

    public double Third;

    }

    static unsafe byte[] YourStructToBytes( YourStruct s[], int arrayLen )

    {

    byte[] arr = new byte[ sizeof(YourStruct) * arrayLen ];

    fixed( byte* parr = arr )

    { * ( (YourStruct * )parr) = s; }

    return arr;

    }

    static unsafe YourStruct[] BytesToYourStruct( byte[] arr, int arrayLen ) {

    if( arr.Length < (sizeof(YourStruct)*arrayLen) )

    throw new ArgumentException();

    YourStruct s[];

    fixed( byte* parr = arr )

    { s = * ((YourStruct * )parr); }

    return s;

    }

    Now you can read bytearray from the file in a single shot and convert back to strucure using BytesToYourStruct

    Hope you can implement this idea and check...

  • I found a potential solution at this site - http://www.eggheadcafe.com/software/aspnet/32846931/writingreading-an-array.aspx

    It says basically to use Binary Formatter like this:

    FileStream fs = new FileStream("DataFile.dat", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, somestruct);

    I also found two questions from this site - http://stackoverflow.com/questions/2871/reading-a-c-c-data-structure-in-c-from-a-byte-array and http://stackoverflow.com/questions/31854/how-to-marshal-an-array-of-structs-net-c-c

    I haven't done this before, being a C# .NET beginner myself. I hope this solution helps.

    batbrat : Sorry pablo, I had computer trouble yesterday. So I couldn't follow up. I am not sure how to achieve what you want. All the best with that.
    batbrat : I hope you find out. If you do, please post an answer here. Thanks.

0 comments:

Post a Comment