title: Write vtk file in binary format
body: <p>I am trying to write a vtk file from my C++ code. I have two versions, one ASCII and the other binary. The ASCII version works well.</p>
  
  <p>I want to do something similar to this post : <a href="https://stackoverflow.com/questions/10913666/error-writing-binary-vtk-files">Error writing binary VTK files</a></p>
  
  <p>Here is my code : </p>
  
  <pre><code>std::ofstream file;
    if(is_binary)
      file.open("test.vtk", std::ios::out | std::ios::binary);
    else
      file.open("test.vtk");
  
    file &lt;&lt; "# vtk DataFile Version 2.0" &lt;&lt; std::endl
         &lt;&lt; "Comment if needed" &lt;&lt; std::endl;
  
    if(is_binary)
      file &lt;&lt; "BINARY"&lt;&lt; std::endl &lt;&lt; std::endl;
    else
      file &lt;&lt; "ASCII"&lt;&lt; std::endl &lt;&lt; std::endl;
  
    file &lt;&lt; "DATASET POLYDATA" &lt;&lt; std::endl &lt;&lt; "POINTS " &lt;&lt; nb_particles &lt;&lt; " float"  &lt;&lt; std::endl;
    for(size_t cell_i=0;cell_i&lt;n_cells;cell_i++)
        for(size_t i =0; i&lt;cells[cell_i].size();++i)
          {
            if(is_binary)
              {
                double rx = cells[cell_i][field::rx][i];
                double ry = cells[cell_i][field::ry][i];
                double rz = cells[cell_i][field::rz][i];
  
                SwapEnd(rx);
                SwapEnd(ry);
                SwapEnd(rz);
  
                file.write(reinterpret_cast&lt;char*&gt;(&amp;rx), sizeof(double));
                file.write(reinterpret_cast&lt;char*&gt;(&amp;ry), sizeof(double));
                file.write(reinterpret_cast&lt;char*&gt;(&amp;rz), sizeof(double));
              }
            else
              {
                Vec3d tmp = grid.particle_position(cell_i,i);
                file &lt;&lt; tmp.x &lt;&lt; " " &lt;&lt; tmp.y &lt;&lt; " " &lt;&lt; tmp.z &lt;&lt; std::endl;
              }
          }
    if(is_binary)
      file &lt;&lt; std::endl;
  
    file &lt;&lt; "POINT_DATA " &lt;&lt; nb_particles &lt;&lt; std::endl;
  
    if(has_id_field)
      {
        file&lt;&lt; "SCALARS index int 1" &lt;&lt; std::endl&lt;&lt; "LOOKUP_TABLE default" &lt;&lt; std::endl;
        for(size_t cell_i=0;cell_i&lt;n_cells;cell_i++)
          for(size_t i =0; i&lt;cells[cell_i].size();++i)
            {
              if(is_binary)
                {
                  uint64_t id = cells[cell_i][field::id][i];
  
                  SwapEnd(id);
  
                  file.write(reinterpret_cast&lt;char*&gt;(&amp;id), sizeof(uint64_t));
                }
              else
                file &lt;&lt; cells[cell_i][field::id][i] &lt;&lt; std::endl;
            }
        if(is_binary)
          file &lt;&lt; std::endl;
      }
  
    if(has_type_field)
      {
        file&lt;&lt; "SCALARS type int 1" &lt;&lt; std::endl&lt;&lt; "LOOKUP_TABLE default" &lt;&lt; std::endl;
        for(size_t cell_i=0;cell_i&lt;n_cells;cell_i++)
          for(size_t i =0; i&lt;cells[cell_i].size();++i)
            {
              if(is_binary)
                {
                  uint8_t type;
  
                  SwapEnd(type);
  
                  file.write(reinterpret_cast&lt;char*&gt;(&amp;type), sizeof(uint8_t));
                }
              else
                file &lt;&lt; static_cast&lt;int&gt;(cells[cell_i][field::type][i]) &lt;&lt; std::endl;
            }
        if(is_binary)
          file &lt;&lt; std::endl;
      }
  
    file.close();
  </code></pre>
  
  <p>with the function : </p>
  
  <pre><code>void SwapEnd(T&amp; var)
  {
    char* varArray = reinterpret_cast&lt;char*&gt;(&amp;var);
    for(long i = 0; i &lt; static_cast&lt;long&gt;(sizeof(var)/2); i++)
      std::swap(varArray[sizeof(var) - 1 - i],varArray[i]);
  }
  </code></pre>
  
  <p>My system is little endian :</p>
  
  <pre><code>lscpu | grep "Byte Order"
  Byte Order:            Little Endian
  </code></pre>
  
  <p>paraview make an error on the reading the output file :</p>
  
  <pre><code>vtkPolyDataReader (0x3863800): Unrecognized keyword: @aic�9h��8
  </code></pre>
  
  <p>What am I not doing well ?</p>