title: Writing aligned data in binary file
body: <p>I am creating a file with some data objects inside. data object have different sizes and are something like this (very simplified):</p>
  
  <pre><code>struct Data{
     uint64_t size;
     char     blob[MAX_SIZE];
  // ... methods here:
  }
  </code></pre>
  
  <p>At some later step, the file will be mmap() in memory,
  so I want the beginning of every data objects to starts on memory address aligned by 8 bytes where <code>uint64_t size</code> will be stored (let's ignore endianness).</p>
  
  <p>Code looks more or less to this (currently hardcoded 8 bytes):</p>
  
  <pre><code>size_t calcAlign(size_t const value, size_t const align_size){
      return align_size - value % align_size;
  }
  
  template&lt;class ITERATOR&gt;
  void process(std::ofstream &amp;file_data, ITERATOR begin, ITERATOR end){
      for(auto it = begin; it != end; ++it){
          const auto &amp;data = *it;
  
          size_t bytesWriten = data.writeToFile(file_data);
  
          size_t const alignToBeAdded = calcAlign(bytesWriten, 8);
  
          if (alignToBeAdded != 8){
              uint64_t const placeholder = 0;
  
              file_data.write( (const char *) &amp; placeholder, (std::streamsize) alignToBeAdded);
          }
      }
  }
  </code></pre>
  
  <p>Is this the best way to achieve alignment inside a file?</p>