title: How to use istream with strings
body: <p>I would like to read an file into a string. I am looking for different ways for how to do it efficiently.</p>
  
  <p><strong>Using a fixed size *char buffer</strong></p>
  
  <p>I have received an <a href="https://stackoverflow.com/questions/6487693/code-for-searching-for-a-string-in-a-binary-file/6487893#6487893">answer</a> from Tony what creates a 16 kb buffer and reads into that buffer and appends the buffer till there is nothing more to read. I understand how it works and I found it very fast. What I don't understand is that in the comments of that answer it is said that this way copies everything twice. But as I understand it, it only happens in the memory, not from the disk, so it is almost unnoticable. Is it a problem that it copies from the buffer to the string in the memory?</p>
  
  <p><strong>Using istreambuf_iterator</strong> </p>
  
  <p>The <a href="https://stackoverflow.com/questions/6487693/code-for-searching-for-a-string-in-a-binary-file/6487928#6487928">other answer</a> I received uses istreambuf_iterator. The code looks beautiful and minimal, but it is extremely slow. I don't know why does it happen. Why are those iterators so slow?</p>
  
  <p><strong>Using memcpy()</strong></p>
  
  <p>For <a href="https://stackoverflow.com/questions/6447688/how-to-delete-parts-from-a-binary-file-in-c">this question</a> I received comments that I should use memcpy() as it is the fastest native method. But how can I use memcpy() with a string and an ifstream object? Isn't ifstream supposed to work with its own read function? Why does using memcpy() ruin portability? I am looking for a solution which is compatible with VS2010 as well as GCC. Why would memcpy() not work with those?</p>
  
  <p><strong>+ Any other efficient way possible?</strong></p>
  
  <p>What do you recommend, what shell I use, for small &lt; 10 MB binary files?</p>
  
  <p><em>(I did not want to split this question in parts, as I am more interested in the comparison between the different way how can I read an ifstream into a string)</em> </p>