[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Monotone-devel] Re: OT: c++ filebuf question
From: |
Václav Haisman |
Subject: |
Re: [Monotone-devel] Re: OT: c++ filebuf question |
Date: |
Tue, 17 Feb 2009 11:47:51 +0100 |
User-agent: |
Thunderbird 2.0.0.19 (Windows/20081209) |
Zack Weinberg wrote:
> On Mon, Feb 16, 2009 at 6:54 PM, Zack Weinberg <address@hidden> 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 address@hidden about
this. The conclusion/benchmark is at <http://codepad.org/RpU97FoF>.
--
VH