title: Issues saving double as binary in c++
body: <p>In my simulation code for a particle system, I have a class defined for particles, and each particle has a property of <code>pos</code> containing its position, which is a <code>double pos[3];</code> as there are 3 coordinate components per particle. So with particle object defined by <code>particles = new Particle[npart];</code> (as we have <code>npart</code> many particles), then e.g. the y-component of the 2nd particle would be accessed with <code>double dummycomp = particles[1].pos[1];</code> </p>
  
  <p>To save the particles to file before using binary I would use (saved as txt, with float precision of <code>10</code> and one particle per line):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  ofstream outfile("testConfig.txt", ios::out);
  outfile.precision(10);
  
    for (int i=0; i&lt;npart; i++){
      outfile &lt;&lt; particle[i].pos[0] &lt;&lt; " " &lt;&lt; particle[i].pos[1]  &lt;&lt; " " &lt;&lt; particle[i].pos[2] &lt;&lt; endl;
  }
  outfile.close();
  </code></pre>
  
  <p>But now, to save space, I am trying to save the configuration as a binary file, and my attempt, inspired from <a href="https://stackoverflow.com/questions/1090428/how-to-output-array-of-doubles-to-hard-drive">here</a>, has been as follows:</p>
  
  <pre><code>ofstream outfile("test.bin", ios::binary | ios::out);
  
  for (int i=0; i&lt;npart; i++){ 
  outfile.write(reinterpret_cast&lt;const char*&gt;(particle[i].pos),streamsize(3*sizeof(double))); 
  }
  outfile.close();
  </code></pre>
  
  <p>but I am facing a segmentation fault when trying to run it. My questions are:</p>
  
  <ul>
  <li>Am I doing something wrong with <code>reinterpret_cast</code> or rather in the argument of <code>streamsize()</code>?</li>
  <li>Ideally, it would be great if the saved binary format could also be read within Python, is my approach (once fixed) allowing for that?</li>
  </ul>
  
  <hr>
  
  <p><strong>working example for the old saving approach (non-binary):</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  class Particle {
  
   public:
  
    double pos[3];
  
  };
  
  
  int main() {
  
    int npart = 2;
    Particle particles[npart];
    //initilizing the positions:
    particles[0].pos[0] = -74.04119568;
    particles[0].pos[1] = -44.33692582;
    particles[0].pos[2] = 17.36278231;
  
    particles[1].pos[0] = 48.16310086;
    particles[1].pos[1] = -65.02325252;
    particles[1].pos[2] = -37.2053818;
  
    ofstream outfile("testConfig.txt", ios::out);
    outfile.precision(10);
  
      for (int i=0; i&lt;npart; i++){
        outfile &lt;&lt; particles[i].pos[0] &lt;&lt; " " &lt;&lt; particles[i].pos[1]  &lt;&lt; " " &lt;&lt; particles[i].pos[2] &lt;&lt; endl;
    }
    outfile.close();
  
      return 0;
  }
  </code></pre>
  
  <p>And in order to save the particle positions as binary, substitute the saving portion of the above sample with </p>
  
  <pre><code>  ofstream outfile("test.bin", ios::binary | ios::out);
  
    for (int i=0; i&lt;npart; i++){
    outfile.write(reinterpret_cast&lt;const char*&gt;(particles[i].pos),streamsize(3*sizeof(double))); 
    }
    outfile.close();
  </code></pre>
  
  <hr>
  
  <p><strong>2nd addendum: reading the binary in Python</strong></p>
  
  <p>I managed to read the saved binary in python as follows using numpy:</p>
  
  <pre><code>data = np.fromfile('test.bin', dtype=np.float64)
  data
  array([-74.04119568, -44.33692582,  17.36278231,  48.16310086,
         -65.02325252, -37.2053818 ])
  </code></pre>
  
  <p>But given the doubts cast in the comments regarding non-portability of binary format, I am not confident this type of reading in Python will always work! It would be really neat if someone could elucidate on the reliability of such approach.</p>