473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

istream_iterator & copying files

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

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

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #1
10 2778
Alex Vinokur wrote:
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.

Jul 22 '05 #2

"Rolf Magnus" <ra******@t-online.de> wrote in message news:c6*************@news.t-online.com...
Alex Vinokur wrote:
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.


Any sample? Thanks.

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #3
Alex Vinokur wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message news:c6*************@news.t-online.com...
Alex Vinokur wrote:

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.

Any sample? Thanks.


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

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

infile >> std::noskipws;

std::copy( in, eos, out );
}
Jul 22 '05 #4

"Jeff Schwab" <je******@comcast.net> wrote in message news:ap********************@comcast.com...
Alex Vinokur wrote:
"Rolf Magnus" <ra******@t-online.de> wrote in message news:c6*************@news.t-online.com...
Alex Vinokur wrote:
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.

Any sample? Thanks.


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

int main( )
{
std::ofstream outfile( "out" )
std::ifstream infile( "in" );
std::istream_iterator< char > in( infile ), eos;
std::ostream_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.....perfometer/43
http://article.gmane.org/gmane.comp.....perfometer/42

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #5
Alex Vinokur wrote:


Thanks.

The testsuite has been added to C/C++ Performance Tests
(Comparative performance measurement : Copying files)
http://article.gmane.org/gmane.comp.....perfometer/43
http://article.gmane.org/gmane.comp.....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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #6

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message news:5j***************@newsread2.news.pas.earthlin k.net...
Alex Vinokur wrote:


Thanks.

The testsuite has been added to C/C++ Performance Tests
(Comparative performance measurement : Copying files)
http://article.gmane.org/gmane.comp.....perfometer/43
http://article.gmane.org/gmane.comp.....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.

[snip]

Thanks.
Binary mode essentially improves performance.

http://article.gmane.org/gmane.comp.....perfometer/45
http://article.gmane.org/gmane.comp.....perfometer/44

--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html


Jul 22 '05 #7

"Jeff Schwab" <je******@comcast.net> wrote in message news:ap********************@comcast.com...
[snip]
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iterator>

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

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


--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #8
On Mon, 19 Apr 2004 14:22:26 +0300, "Alex Vinokur"
<al****@big.foot.com> wrote:
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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #9
On Tue, 20 Apr 2004 14:47:57 +0300, "Alex Vinokur"
<al****@big.foot.com> wrote:
-------------------------------
std::copy( in, eos, out );

Is it possible to that via transform()?


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
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #10

"tom_usenet" <to********@hotmail.com> wrote in message news:vv********************************@4ax.com...
On Tue, 20 Apr 2004 14:47:57 +0300, "Alex Vinokur"
<al****@big.foot.com> wrote:
-------------------------------
std::copy( in, eos, out ); Is it possible to that via transform()?


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());
---------------------------

copy-method and transform-method have almost the same performance.
It seems that copy-method is a bit better, but perhaps is a metering error.
--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
4
by: Bill Rudolph | last post by:
The member function basic_ios::operator!() returns the bool result of the basic_ios::fail() function which is true if either failbit or badbit is set (This is per p. 616 of TC++PL by B. Stroustrup...
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
9
by: Alex Vinokur | last post by:
------ foo.cpp : BEGIN ------ #include <cassert> #include <vector> #include <string> #include <iostream> #include <iterator> #include <fstream> using namespace std;
6
by: George | last post by:
Hi All, I'm trying to learn c++/stl. I'd like a fancy way to read lines of an ascii file into vector of stringbufs. I made a first attempt, but the compiler complains about private...
3
by: jmoy.matecon | last post by:
I get an error while compiling the following program: int main() { vector<int> v(istream_iterator<int>(cin), istream_iterator<int>()); copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\n"));...
5
by: Pradeep | last post by:
Hi All, I am facing some problem using istream_iterator for reading the contents of a file and copying it in a vector of strings.However the same thing works for a vector of integers. The...
2
by: Juha Nieminen | last post by:
I'm using gcc 3.3.5. This code: std::set<std::stringt(std::istream_iterator<std::string>(std::cin), std::istream_iterator<std::string>()); gives a strange error message: error: cannot use...
4
by: Jim Barlow | last post by:
Does anyone know why K&R2 uses the term "File Copying" at this point (1.5.1)? Also, in the K&R2 answers to exercises maintained by Richard Heathfield, for Listing KRX113 Mr Heathfield repeatedly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.