Discussion:
best way of copy istream to ostream
(too old to reply)
bg
2006-04-26 13:35:10 UTC
Permalink
hi all,

what the best (stl) way of copy input to output using ofstreams/ifstreams

I currently have something like this

ofstream output(output_file_name.c_str(), ios_base::out | ios_base::trunc |
ios_base::binary);
// ... for each file ...
ifstream input(inputname.c_str(),ios_base::in|ios_base::binary);
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);

input.close();

This strikes me as rather clunky and inelegant, there must be a better way,
std::copy? istream iteration ?

Any thoughts appreciated.

TIA

bg
Pete Becker
2006-04-26 14:06:11 UTC
Permalink
Post by bg
hi all,
what the best (stl) way of copy input to output using ofstreams/ifstreams
Depends on what you mean by that "(stl)" constraint. If you mean
Standard Template Library, then maybe:

copy(istream_iterator<unsigned char>(input), istream_iterator<unsigned
char.(), ostream_iterator<unsigned char>(output, ""));

If it doesn't mean anything, as is often the case, then:

output << input.rdbuf();
--
Pete Becker
Roundhouse Consulting, Ltd.
Tom Widmer [VC++ MVP]
2006-04-26 14:15:18 UTC
Permalink
Post by Pete Becker
Post by bg
hi all,
what the best (stl) way of copy input to output using ofstreams/ifstreams
Depends on what you mean by that "(stl)" constraint. If you mean
copy(istream_iterator<unsigned char>(input), istream_iterator<unsigned
char.(), ostream_iterator<unsigned char>(output, ""));
Don't you mean:
copy(
istreambuf_iterator<char>(input),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(output)
);

Tom
Pete Becker
2006-04-26 14:26:55 UTC
Permalink
Post by Tom Widmer [VC++ MVP]
Post by Pete Becker
copy(istream_iterator<unsigned char>(input), istream_iterator<unsigned
char.(), ostream_iterator<unsigned char>(output, ""));
copy(
istreambuf_iterator<char>(input),
istreambuf_iterator<char>(),
ostreambuf_iterator<char>(output)
);
That would also work.
--
Pete Becker
Roundhouse Consulting, Ltd.
Ulrich Eckhardt
2006-04-26 14:08:24 UTC
Permalink
Post by bg
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.
Post by bg
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.
Post by bg
ifstream input(inputname.c_str(),ios_base::in|ios_base::binary);
The ios_base::in is default, too.
Post by bg
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'!
Post by bg
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
bg
2006-04-26 15:13:48 UTC
Permalink
cheers all,

output << input.rdbuff();

was what i was thinking of, and it works a charm to. its just as quick as
are other "OS based" stuff as well.

thanks

bg

Loading...