istream_iterator & copying files

Discussion in 'C++' started by Alex Vinokur, Apr 19, 2004.

  1. Alex Vinokur

    Alex Vinokur Guest

    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:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 19, 2004
    #1
    1. Advertising

  2. Alex Vinokur

    Rolf Magnus Guest

    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.
    Rolf Magnus, Apr 19, 2004
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "Rolf Magnus" <> wrote in message news:c60ea4$gda$04$-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:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 19, 2004
    #3
  4. Alex Vinokur

    Jeff Schwab Guest

    Alex Vinokur wrote:
    > "Rolf Magnus" <> wrote in message news:c60ea4$gda$04$-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::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 );
    }
    Jeff Schwab, Apr 19, 2004
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    "Jeff Schwab" <> wrote in message news:...
    > Alex Vinokur wrote:
    > > "Rolf Magnus" <> wrote in message news:c60ea4$gda$04$-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::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

    --
    Alex Vinokur
    mailto:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 19, 2004
    #5
  6. 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.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
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Apr 19, 2004
    #6
  7. Alex Vinokur

    Alex Vinokur Guest

    "Kevin Goodsell" <> wrote in message news:5jVgc.2535$...
    > 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.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.
    >

    [snip]

    Thanks.
    Binary mode essentially improves performance.

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

    --
    Alex Vinokur
    mailto:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 20, 2004
    #7
  8. Alex Vinokur

    Alex Vinokur Guest

    "Jeff Schwab" <> wrote in message news:...
    [snip]
    > #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()?
    -------------------------------
    > }


    --
    Alex Vinokur
    mailto:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 20, 2004
    #8
  9. Alex Vinokur

    tom_usenet Guest

    On Mon, 19 Apr 2004 14:22:26 +0300, "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'?


    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
    tom_usenet, Apr 20, 2004
    #9
  10. Alex Vinokur

    tom_usenet Guest

    On Tue, 20 Apr 2004 14:47:57 +0300, "Alex Vinokur"
    <> 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
    tom_usenet, Apr 20, 2004
    #10
  11. Alex Vinokur

    Alex Vinokur Guest

    "tom_usenet" <> wrote in message news:...
    > On Tue, 20 Apr 2004 14:47:57 +0300, "Alex Vinokur"
    > <> 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:
    http://mathforum.org/library/view/10978.html
    Alex Vinokur, Apr 20, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Rudolph

    istream_iterator question

    Bill Rudolph, Aug 14, 2003, in forum: C++
    Replies:
    4
    Views:
    548
    Bill Rudolph
    Aug 15, 2003
  2. Chris Mantoulidis
    Replies:
    2
    Views:
    413
  3. NPC

    istream_iterator<>

    NPC, May 13, 2004, in forum: C++
    Replies:
    3
    Views:
    1,289
    tom_usenet
    May 14, 2004
  4. Alex Vinokur

    Copying with istream_iterator

    Alex Vinokur, Jul 21, 2004, in forum: C++
    Replies:
    9
    Views:
    580
    Alex Vinokur
    Jul 24, 2004
  5. ben

    about istream_iterator

    ben, May 17, 2005, in forum: C++
    Replies:
    2
    Views:
    416
Loading...

Share This Page