monotone-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Monotone-devel] Re: OT: c++ filebuf question


From: Zack Weinberg
Subject: [Monotone-devel] Re: OT: c++ filebuf question
Date: Mon, 16 Feb 2009 19:11:21 -0800

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;

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.

zw




reply via email to

[Prev in Thread] Current Thread [Next in Thread]