title: Binary stream or something like this to save class like std::vector to a file?
body: <p>I'm not good in IOstream library since I have accustom to stdio and stuff life this, however I got a problem I hoped to be solved in IOstream but I find that it probably not. So I'm quite new to standard C++ libraries but quite well with C++ OOP/Classes and so on.</p>
  
  <p>So I can't use code like</p>
  
  <pre><code>printf (stream, "...", C);
  </code></pre>
  
  <p>if C is of an aggregate type because I can't create new format string options like %mytupe. Also I can't expect proper behavior of</p>
  
  <pre><code>fwrite/fread (&amp;C, sizeof(C), 1, stream)
  </code></pre>
  
  <p>if T contains fields that are pointers because fwrite/fread will save/load value of a pointer but not a value stored in memory where the pointer refers to:</p>
  
  <pre><code>class MyClass
     {...
      private:
         {typename} Tp* Data;
     } C;
  </code></pre>
  
  <p>I don't care much of first limit because I can write a function that convert object of each of my class to a text string, it works even if but the last can't be solved easily. For example, I tried to create a function that save each class to binary file but I got a lot of problems with staff like luck of partial specialization of a template and so on (mo matter).</p>
  
  <p>Being tired of making bugs and mistakes while rewriting standard code (like own string and file holder classes) I hoped that learning (at last!) of standard (written by clever people and well-tested :) library will help me  since I read a lot that standard C++ library solve first issue with using of streams. I can overload operator &lt;&lt; and operator >> or so on to be sure that my class will be saved to or read from text file properly. But what about binary files which is much much more important for me? </p>
  
  <p>What should I do if I want to save an object of class like vector, for example, to the binary file? Using of &lt;&lt; and >> fails at all since it says that vector has no operators &lt;&lt; and >> overloaded, but even if it had it would produce text data.</p>
  
  <p>Staff like</p>
  
  <pre><code>vector &lt;MyClass&gt; V;
  ...
  ofstream file ("file.bin", ios::binary);
  
  int size1 = ;
  file.write((const char*)&amp;V.size(), sizeof(V.size()));
  file.write((const char*)&amp;V[0], V.size() * sizeof(MyClass));
  </code></pre>
  
  <p>is not suitable (and doesn't differs much from using of fwrite) since it saves value (address) of pointer field but not the data stored there (also, what if I declare a "two-dimension" vector as vector  > ??). So, if there was overloading of vector operator &lt;&lt; like</p>
  
  <pre><code>template &lt;class T&gt; vector
    {public:
     ...    
       ostream operator &lt;&lt; () const
          {ostream s;
           for (uint32_t k = 0; k &lt; size(); k++)
              s &lt;&lt; s &lt;&lt; this-&gt;operator[] (k);
           return s;
          }
     private:
        T* Data;
    };
  </code></pre>
  
  <p>and if each T::operator &lt;&lt; was overloaded too in the same way (for MyClass - to provide stream of data stored in MyCLass::Tp) it was saved. </p>
  
  <p>(I know, I know, there should be iterator, but maybe I made a more serious mistake because of total misunderstanding of streams? Anyway just I'm talking about idea.)</p>
  
  <p>Well, it is a way to convert data to text, not to got binary data as it is stored in memory, but I know there can be written an interface to work with binary data in the same way (maybe not using &lt;&lt; and >> but function names, but it can be for sure)! The questing is: was it done in standard C++ library or somewhere else (another opensource library for C++)? Yes, yes, to properly write a vector to file in one line. (I'll be very surprised if it is not included into standard C++ because how do people save data they work to files if they want to use multidimension dynamic arrays?)</p>