title: Force C++ structure to pack tightly
body: <p>I am attempting to read in a binary file. The problem is that the creator of the file took no time to properly align data structures to their natural boundaries and everything is packed tight. This makes it difficult to read the data using C++ structs.</p>
  
  <p>Is there a way to force a <code>struct</code> to be packed tight?</p>
  
  <p>Example:</p>
  
  <pre><code>struct {
      short a;
      int b;
  }
  </code></pre>
  
  <p>The above structure is 8 bytes: 2 for <code>short a</code>, 2 for padding, 4 for <code>int b</code>. However, on disk, the data is only 6 bytes (not having the 2 bytes of padding for alignment)</p>
  
  <p>Please be aware the actual data structures are thousands of bytes and many fields, including a couple arrays, so I would prefer not to read each field individually.</p>