Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > istream_iterator & copying files

Reply
Thread Tools

istream_iterator & copying files

 
 
Alex Vinokur
Guest
Posts: n/a
 
      04-19-2004
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
(E-Mail Removed)
http://mathforum.org/library/view/10978.html



 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-19-2004
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.

 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      04-19-2004

"Rolf Magnus" <(E-Mail Removed)> wrote in message news:c60ea4$gda$04$(E-Mail Removed)-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
(E-Mail Removed)
http://mathforum.org/library/view/10978.html



 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      04-19-2004
Alex Vinokur wrote:
> "Rolf Magnus" <(E-Mail Removed)> wrote in message news:c60ea4$gda$04$(E-Mail Removed)-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:fstream outfile( "out" )
std::ifstream infile( "in" );
std::istream_iterator< char > in( infile ), eos;
std:stream_iterator< char > out( outfile );

infile >> std::noskipws;

std::copy( in, eos, out );
}
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      04-19-2004

"Jeff Schwab" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Alex Vinokur wrote:
> > "Rolf Magnus" <(E-Mail Removed)> wrote in message news:c60ea4$gda$04$(E-Mail Removed)-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:fstream outfile( "out" )
> std::ifstream infile( "in" );
> std::istream_iterator< char > in( infile ), eos;
> std: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.....perfometer/43
http://article.gmane.org/gmane.comp.....perfometer/42

--
Alex Vinokur
(E-Mail Removed)
http://mathforum.org/library/view/10978.html



 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      04-19-2004
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.
 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      04-20-2004

"Kevin Goodsell" <(E-Mail Removed)> wrote in message news:5jVgc.2535$(E-Mail Removed) 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
(E-Mail Removed)
http://mathforum.org/library/view/10978.html






 
Reply With Quote
 
Alex Vinokur
Guest
Posts: n/a
 
      04-20-2004

"Jeff Schwab" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
[snip]
> #include <algorithm>
> #include <fstream>
> #include <iomanip>
> #include <iterator>
>
> int main( )
> {
> std:fstream outfile( "out" )
> std::ifstream infile( "in" );
> std::istream_iterator< char > in( infile ), eos;
> std:stream_iterator< char > out( outfile );
>
> infile >> std::noskipws;
>

-------------------------------
> std::copy( in, eos, out );

Is it possible to that via transform()?
-------------------------------
> }


--
Alex Vinokur
(E-Mail Removed)
http://mathforum.org/library/view/10978.html



 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      04-20-2004
On Mon, 19 Apr 2004 14:22:26 +0300, "Alex Vinokur"
<(E-Mail Removed)> 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
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      04-20-2004
On Tue, 20 Apr 2004 14:47:57 +0300, "Alex Vinokur"
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about istream_iterator ben C++ 2 05-18-2005 03:39 AM
Copying with istream_iterator Alex Vinokur C++ 9 07-24-2004 06:45 AM
istream_iterator<> NPC C++ 3 05-14-2004 11:41 AM
istream_iterator and ostream_iterator problem Chris Mantoulidis C++ 2 12-15-2003 12:14 PM
istream_iterator question Bill Rudolph C++ 4 08-15-2003 07:28 AM



Advertisments