title: Writing/reading std::complex values in binary file
body: <p>I pretend to store a <code>std::vector</code> of <code>std::complex&lt;float&gt;</code> in a binary file, and to later load that data into a std::vector again. I have checked similar questions in SO and came up with the following code:</p>
  
  <pre><code>#include &lt;complex&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;fstream&gt;
  #include &lt;iterator&gt;
  #include &lt;random&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  typedef vector&lt;complex&lt;float&gt;&gt; complex_vector;
  
  
  float get_random(){
      static std::default_random_engine e;
      static std::uniform_real_distribution&lt;&gt; dis(-10, 10);
      return dis(e);
  }
  
  int main(){
      // Random number generator
      srand(static_cast&lt;unsigned&gt; (time(0)));
  
      // Create a vector with 25 random complex&lt;float&gt; values
      std::shared_ptr&lt;vector&lt;complex&lt;float&gt;&gt;&gt; buffer = std::make_shared&lt;vector&lt;complex&lt;float&gt;&gt;&gt;();
      for(unsigned int i=0; i&lt;25; i++){
          buffer-&gt;push_back(complex&lt;float&gt;(get_random(), get_random()));
      }
  
      // Write those values into a binary file
      std::string binFileName = "aux.bin";
      std::ofstream rawFile(binFileName, std::ios::binary);
      for(unsigned int i=0; i&lt;buffer-&gt;size(); i++){
          rawFile.write(reinterpret_cast&lt;const char*&gt;(&amp;buffer-&gt;at(i)), sizeof(complex&lt;float&gt;));
      }
      rawFile.close();
  
      // Load the binary file into the buffer
      std::ifstream input(binFileName, std::ios::binary);
      complex_vector auxBuffer{std::istream_iterator&lt;complex&lt;float&gt;&gt;(input), std::istream_iterator&lt;complex&lt;float&gt;&gt;()};
      unsigned int samplesRead = auxBuffer.size();
      std::cout &lt;&lt; samplesRead;
  
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>0
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>EDIT:
  After NathanOliver's answer, this is how my code looks like now:</p>
  
  <pre><code>#include &lt;complex&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;fstream&gt;
  #include &lt;iterator&gt;
  #include &lt;random&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  typedef vector&lt;complex&lt;float&gt;&gt; complex_vector;
  
  
  float get_random(){
      static std::default_random_engine e;
      static std::uniform_real_distribution&lt;&gt; dis(-10, 10);
      return dis(e);
  }
  
  int main(){
      // Random number generator
      srand(static_cast&lt;unsigned&gt; (time(0)));
  
      // Create a vector with 25 random complex&lt;float&gt; values
      std::shared_ptr&lt;complex_vector&gt; buffer = std::make_shared&lt;complex_vector&gt;();
      for(unsigned int i=0; i&lt;25; i++){
          buffer-&gt;push_back(complex&lt;float&gt;(get_random(), get_random()));
      }
  
      // Write those values into a binary file
      std::string binFileName = "aux.bin";
      std::ofstream rawFile(binFileName, std::ios::binary);
      rawFile.write(reinterpret_cast&lt;const char*&gt;(buffer-&gt;size()), sizeof(buffer-&gt;size()));
      rawFile.write(reinterpret_cast&lt;const char*&gt;(buffer-&gt;data()), sizeof(std::complex&lt;float&gt;) * buffer-&gt;size());
      rawFile.close();
  
      // Load the binary file into the buffer
      std::ifstream input(binFileName, std::ios::binary);
      complex_vector::size_type nSamples;
      input.read(reinterpret_cast&lt;char*&gt;(nSamples), sizeof(complex_vector::size_type));
      std::cout &lt;&lt; nSamples;
      complex_vector *destination = new complex_vector(25);
      input.read(reinterpret_cast&lt;char*&gt;(destination-&gt;data()), sizeof(std::complex&lt;float&gt;) * nSamples);
  
      return 0;
  }
  </code></pre>
  
  <p>I'm getting a SIGSEGV in the first call to write function.</p>
  
  <p>Also, I don't understand why I write , but I have to read .</p>