Reading through binary files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jón Sveinsson

    #1

    Reading through binary files

    Hello everyone

    I have been able to read data from binary files to
    filestrean, the data in the files are structured, what
    I'm trying to do is to loop through the binary files and
    add data to my struct, in this file there are lot of
    records i need to loop trough, first time I read the
    file, the data is fine but for the rest the data is not
    correct
    here is my code for reading the binary files and loop
    through the code, what is wrong with the code

    public static void Main()
    {
    PRD_record rec = new PRD_record();//struct which
    has been written to file

    FileStream fs = new FileStream
    (@"c:\elas.dat" ,FileMode.Open) ;//Filestream to read text
    TextWriter tw = new StreamWriter
    (@"c:\elas.txt" );//Textwriter to write out text

    byte[] array = new byte[Marshal.SizeOf( rec)];

    while((fs.Read( array,0,Marshal .SizeOf(rec))!= 0))
    {
    rec = BytesToYourStru ct(array);
    tw.WriteLine(re c.abs_pot_no + " " + rec.md.Blev);

    }

    fs.Close();
    tw.Close();

    }


    //Converting my structure to bytes
    static byte[] YourStructToByt es( PRD_record s )
    {

    int size = Marshal.SizeOf( s );
    byte[] retArr = new byte[ size ];
    IntPtr buf = Marshal.AllocHG lobal( size ); // create
    unmanaged memory
    Marshal.Structu reToPtr ( s, buf, false ); // copy
    struct

    for (int i=0; i<size; ++i)
    {
    retArr[i] = Marshal.ReadByt e(buf, i); // read
    unmanaged bytes
    }
    Marshal.FreeHGl obal( buf );
    return retArr;
    }



    //Converting my bytes to struct
    static PRD_record BytesToYourStru ct( byte[] arr )
    {

    PRD_record s;
    IntPtr arrayPointer=Ma rshal.UnsafeAdd rOfPinnedArrayE lement
    (arr,0);
    s=(PRD_record)M arshal.PtrToStr ucture(arrayPoi nter,typeof
    (PRD_record));
    return s;

    }
    }


  • Steven Van Loon

    #2
    Re: Reading through binary files

    I think you should take a look at object serialization. A usefull article
    can be http://www.c-sharpcorner.com/vbnet/o...ialization.asp

    good luck!
    Steven.


    "Jón Sveinsson" <rujonhsv@hotma il.com> wrote in message
    news:058f01c3b8 6c$c84a09e0$a30 1280a@phx.gbl.. .[color=blue]
    > Hello everyone
    >
    > I have been able to read data from binary files to
    > filestrean, the data in the files are structured, what
    > I'm trying to do is to loop through the binary files and
    > add data to my struct, in this file there are lot of
    > records i need to loop trough, first time I read the
    > file, the data is fine but for the rest the data is not
    > correct
    > here is my code for reading the binary files and loop
    > through the code, what is wrong with the code
    >
    > public static void Main()
    > {
    > PRD_record rec = new PRD_record();//struct which
    > has been written to file
    >
    > FileStream fs = new FileStream
    > (@"c:\elas.dat" ,FileMode.Open) ;//Filestream to read text
    > TextWriter tw = new StreamWriter
    > (@"c:\elas.txt" );//Textwriter to write out text
    >
    > byte[] array = new byte[Marshal.SizeOf( rec)];
    >
    > while((fs.Read( array,0,Marshal .SizeOf(rec))!= 0))
    > {
    > rec = BytesToYourStru ct(array);
    > tw.WriteLine(re c.abs_pot_no + " " + rec.md.Blev);
    >
    > }
    >
    > fs.Close();
    > tw.Close();
    >
    > }
    >
    >
    > //Converting my structure to bytes
    > static byte[] YourStructToByt es( PRD_record s )
    > {
    >
    > int size = Marshal.SizeOf( s );
    > byte[] retArr = new byte[ size ];
    > IntPtr buf = Marshal.AllocHG lobal( size ); // create
    > unmanaged memory
    > Marshal.Structu reToPtr ( s, buf, false ); // copy
    > struct
    >
    > for (int i=0; i<size; ++i)
    > {
    > retArr[i] = Marshal.ReadByt e(buf, i); // read
    > unmanaged bytes
    > }
    > Marshal.FreeHGl obal( buf );
    > return retArr;
    > }
    >
    >
    >
    > //Converting my bytes to struct
    > static PRD_record BytesToYourStru ct( byte[] arr )
    > {
    >
    > PRD_record s;
    > IntPtr arrayPointer=Ma rshal.UnsafeAdd rOfPinnedArrayE lement
    > (arr,0);
    > s=(PRD_record)M arshal.PtrToStr ucture(arrayPoi nter,typeof
    > (PRD_record));
    > return s;
    >
    > }
    > }
    >
    >[/color]


    Comment

    Working...