Re: best way of copy istream to ostream
bg wrote:
what the best (stl) way of copy input to output using ofstreams/ifstreams
C++ IOStreams are not part of the STL, you mean the C++ standardlibrary.
ofstream output(output_file_name.c_str(), ios_base::out | ios_base::trunc
| ios_base::binary);
The ios_base::out and the ios_base::trunc are the defaults already.
ifstream input(inputname.c_str(),ios_base::in|ios_base::binary);
The ios_base::in is default, too.
do
{
// copy chunks of the input to the output
// until theres nothing more to read
input.read(buf, buffsize);
tot = input.gcount();
if(tot>0) output.write(buf,tot);
} while(tot>0);
There is an overloaded << operator that takes a streambuffer:
out << in.rdbuf();
// TODO: errorhandling for 'in' and 'out'!
This strikes me as rather clunky and inelegant, there must be a better
way, std::copy? istream iteration ?
The above is probably the shortest and most straightforward way using
IOStreams, but it is still clunky when compared to a platform-specific
solution. The point is that the streams still have to filter each byte
through a complicated IO mechanism, none of which is needed for your job.
I'd only use this as a default implementation for platforms that don't
provide any really straightforward means.
Uli