Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. reading binary files

reading binary files

Scheduled Pinned Locked Moved C#
csharpc++help
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Crazy Joe Devola
    wrote on last edited by
    #1

    I am trying to write a C# program to read a binary file. The records have a predefined strucutre. Problem is I have no idea to to define the struct in C# in the way I did in C++. I also need the strucutre to be in 1 byte alignment. In C++ the structure looks as follows: #pragma pack(1) struct my_struct_ { unsigned char fld1[10]; long fld2; short fld3; }; #pragma pack() I tried the MSDN but the examples looks so complicated I can't understand a thing.. :-(

    P 1 Reply Last reply
    0
    • C Crazy Joe Devola

      I am trying to write a C# program to read a binary file. The records have a predefined strucutre. Problem is I have no idea to to define the struct in C# in the way I did in C++. I also need the strucutre to be in 1 byte alignment. In C++ the structure looks as follows: #pragma pack(1) struct my_struct_ { unsigned char fld1[10]; long fld2; short fld3; }; #pragma pack() I tried the MSDN but the examples looks so complicated I can't understand a thing.. :-(

      P Offline
      P Offline
      Polis Pilavas
      wrote on last edited by
      #2

      Take a look here[^] for a good tutorial on C# structures. Regards, Polis Can you practice what you teach?

      C 1 Reply Last reply
      0
      • P Polis Pilavas

        Take a look here[^] for a good tutorial on C# structures. Regards, Polis Can you practice what you teach?

        C Offline
        C Offline
        Crazy Joe Devola
        wrote on last edited by
        #3

        Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?

        P D 2 Replies Last reply
        0
        • C Crazy Joe Devola

          Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?

          P Offline
          P Offline
          Polis Pilavas
          wrote on last edited by
          #4

          You can declare an array of bytes like this:

          byte[] buffer = new byte[1];

          Regards, Polis Can you practice what you teach?

          C 1 Reply Last reply
          0
          • C Crazy Joe Devola

            Thanks for that. However, I still don't know to force 1 byte strucutre member alignment and array of bytes?

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Packing size is done as follows:

            [StructLayout(LayoutKind.Sequential, Pack=1)]
            public struct WhoCares
            {
            public int x;
            public int y;
            }

            RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            S 1 Reply Last reply
            0
            • P Polis Pilavas

              You can declare an array of bytes like this:

              byte[] buffer = new byte[1];

              Regards, Polis Can you practice what you teach?

              C Offline
              C Offline
              Crazy Joe Devola
              wrote on last edited by
              #6

              Thanks, but I am not sure that's what I need. In C++, my structure looks as follows: struct sample_ { unsigned short f1; long f2; unsigned char filler[1000]; }; // total 1006 I declared it in C# as follows: [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)] public class sample_ { public ushort f1; public int f2; public byte[] arr=new byte[1000]; } But it's not good because the size should 1006, and it is actually 10 (2+4+4). sample_ mySample = new sample_(); Console.WriteLine ("sizeof={0}", Marshal.SizeOf(mySample)); Is there a way I can declare the array without new? Or do I have to declare 1000 byte fillers?

              1 Reply Last reply
              0
              • D Dave Kreskowiak

                Packing size is done as follows:

                [StructLayout(LayoutKind.Sequential, Pack=1)]
                public struct WhoCares
                {
                public int x;
                public int y;
                }

                RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                S Offline
                S Offline
                suceny
                wrote on last edited by
                #7

                I got the same problem.at last ,I found a question that the size of the struct is very complex as we thought.But there is a way to read the blok of memory that stored as struct type.Maybe we can disguss it together.

                D 1 Reply Last reply
                0
                • S suceny

                  I got the same problem.at last ,I found a question that the size of the struct is very complex as we thought.But there is a way to read the blok of memory that stored as struct type.Maybe we can disguss it together.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Not until you start your own question as a new thread. I'm not responding to someone who thinks they can just hyjack someone elses 4 year old thread and I'l magically know what they're talking about.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008
                  But no longer in 2009...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups