title: How to Write Strings Into Binary File
body: <p>With this code I tried to print the string "foo" 10 times
  in binary format. But why doesn't the function to do it work?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  template &lt;typename T&gt; void WriteStr2BinFh (string St, ostream &amp;fn) {
       for (unsigned i = 0; i &lt; St.size(); i++) {
           char CStr = St[i];
           fn.write(&amp;CStr.front(), CStr.size());
       }
       return;
  }
  
  int main() {
     string MyStr = "Foo";
     ofstream myfile;
     myfile.open("OuputFile.txt", ios::binary|ios::out);
  
     // We want to print it 10 times horizontally
     // separated with tab
  
    for (int i = 0; i &lt; 9; i++) {
        WriteStr2BinFh(Mystr+"\t", myfile);
     }
  
     myfile.close();   
  }
  </code></pre>