istream_iterator & copying files

A

Alex Vinokur

ofstream outfile ("out");
ifstream infile ("in");
istream_iterator<char> iter(infile), eos;

Is it possible to copy 'infile' to 'outfile' using 'iter' and 'eos'?
 
R

Rolf Magnus

Alex said:
ofstream outfile ("out");
ifstream infile ("in");
istream_iterator<char> iter(infile), eos;

Is it possible to copy 'infile' to 'outfile' using 'iter' and 'eos'?

Yes.
 
J

Jeff Schwab

Alex said:
Any sample? Thanks.

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iterator>

int main( )
{
std::eek:fstream outfile( "out" )
std::ifstream infile( "in" );
std::istream_iterator< char > in( infile ), eos;
std::eek:stream_iterator< char > out( outfile );

infile >> std::noskipws;

std::copy( in, eos, out );
}
 
A

Alex Vinokur

Jeff Schwab said:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iterator>

int main( )
{
std::eek:fstream outfile( "out" )
std::ifstream infile( "in" );
std::istream_iterator< char > in( infile ), eos;
std::eek:stream_iterator< char > out( outfile );

infile >> std::noskipws;

std::copy( in, eos, out );
}

Thanks.

The testsuite has been added to C/C++ Performance Tests
(Comparative performance measurement : Copying files)
http://article.gmane.org/gmane.comp.lang.c++.perfometer/43
http://article.gmane.org/gmane.comp.lang.c++.perfometer/42
 
K

Kevin Goodsell

Alex said:
Thanks.

The testsuite has been added to C/C++ Performance Tests
(Comparative performance measurement : Copying files)
http://article.gmane.org/gmane.comp.lang.c++.perfometer/43
http://article.gmane.org/gmane.comp.lang.c++.perfometer/42

If you want a fast way to copy files in C++, it should probably look
something like this:

ifstream infile("in", ios::binary);
ofstream outfile("out", ios::binary);
outfile << infile.rdbuf();

The iterator version includes considerable overhead compared to this
version.

-Kevin
 
A

Alex Vinokur

A

Alex Vinokur

Jeff Schwab said:
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iterator>

int main( )
{
std::eek:fstream outfile( "out" )
std::ifstream infile( "in" );
std::istream_iterator< char > in( infile ), eos;
std::eek:stream_iterator< char > out( outfile );

infile >> std::noskipws;
-------------------------------
std::copy( in, eos, out );
Is it possible to that via transform()?
-------------------------------
 
T

tom_usenet

ofstream outfile ("out");
ifstream infile ("in");
istream_iterator<char> iter(infile), eos;

Is it possible to copy 'infile' to 'outfile' using 'iter' and 'eos'?

istream_iterator<char> skips whitespace - it uses a formatted read
operation. You should open the files in binary mode, and use the
unformatted iterators:

istreambuf_iterator<char> iter(infile), eos;
ostreambuf_iterator<char> out(outfile);
//make sure serious errors are reported
infile.exceptions(ios_base::bad);
outfile.exceptions(ios_base::bad | ios_base::fail | ios_base::eof);
std::copy(iter, eos, out);

Tom
 
T

tom_usenet


std::copy is equivalent to std::transform with the identity function.
So what would be the point of using std::transform where std::copy
suffices?

(if you must:

template <class T>
struct identity
{
T operator(T t) const
{
return t;
}
};

Then
std::transform(in, eos, out, identity<char>());
)

Tom
 
A

Alex Vinokur

tom_usenet said:
std::copy is equivalent to std::transform with the identity function.
So what would be the point of using std::transform where std::copy
suffices?

To compare their performances.
(if you must:

template <class T>
struct identity
{
T operator(T t) const
{
return t;
}
};

Then
std::transform(in, eos, out, identity<char>());
)
[snip]

OK.

---------------------------
istreambuf_iterator<char> in(fs_in), eos;
ostreambuf_iterator<char> out(fs_in);
fs_bin_in >> noskipws;

// copy-method
copy (in, eos, out);

// transform-method
struct char_identity
{
char operator()(char ch) const { return ch; }
};
transform(in, eos, out, char_identity());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top