Zack Weinberg wrote:
> On Mon, Feb 16, 2009 at 6:54 PM, Zack Weinberg <[email protected]> wrote:
>> As far as I can tell, this is the most efficient way in standard C++
>> to write the entire contents of a string to a file:
>>
>>  std::copy(data.begin(), data.end(), ostreambuf_iterator<char>(&fout));
> 
> It occurs to me shortly after hitting send that this is rather more
> verbose and probably no faster than
> 
>   fout << data;
If you can assume continuous memory, I think fout.write (data.data (),
data.size ()) is faster.

> 
> where fout is now an ofstream.  And I know how to detect I/O errors
> with an actual ofstream.  I wrote it this way because in a different
> function in the same file there's this:
> 
>   filebuf fin;
>   if (!fin.open(fname, ios::in))
>     return false;
> 
>   result.clear();
>   copy(istreambuf_iterator<char>(&fin), istreambuf_iterator<char>(),
>        back_inserter(result));
> 
> which *really is* the most efficient way to copy the entire contents
> of a file *into* a std::string as far as I know.  Only I don' t know
> how to detect I/O errors on *that*, either.
> 
> So I'm really requesting enlightenment as to (a) how to detect I/O
> errors with filebufs and [io]streambuf_iterators generally, (b) the
> real true actual most efficient way to copy the entire contents of a
> file into a string, and back out again.
Ad (b), not so long ago there was a discussion in ##...@freenode about
this. The conclusion/benchmark is at <http://codepad.org/RpU97FoF>.


--
VH


_______________________________________________
Monotone-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/monotone-devel

Reply via email to