concatenating binary files

Discussion in 'C++' started by Igor R., Jan 15, 2009.

  1. Igor R.

    Igor R. Guest

    What's the laconic and standard way to do the subj - without an
    explicit loop of [allocate-read-write]? In case of text files, I'd do
    something like:
    ifstream in("file1.txt", ios::in);
    ofstream out("file2.txt", ios::eek:ut | ios::ate);
    out << in.rdbuf();
    But in case of binary files, I guess "<<" would do some undesired
    formatting.
    Igor R., Jan 15, 2009
    #1
    1. Advertising

  2. Igor R.

    Ron AF Greve Guest

    Hi,


    "Igor R." <> wrote in message
    news:...
    > What's the laconic and standard way to do the subj - without an
    > explicit loop of [allocate-read-write]? In case of text files, I'd do
    > something like:
    > ifstream in("file1.txt", ios::in);
    > ofstream out("file2.txt", ios::eek:ut | ios::ate);
    > out << in.rdbuf();
    > But in case of binary files, I guess "<<" would do some undesired
    > formatting.


    Open the files with ios_base::binary
    and then use

    char Buffer[ SomeSize ];

    in.read( Buffer, sizeof( Buffer ) );
    out.write( Buffer, in.gcount() );

    In a loop.

    Regards, Ron AF Greve

    http://informationsuperhighway.eu/
    Ron AF Greve, Jan 15, 2009
    #2
    1. Advertising

  3. Igor R.

    Guest

    On Jan 15, 9:19 am, "Igor R." <> wrote:
    > What's the laconic and standard way to do the subj - without an
    > explicit loop of [allocate-read-write]?



    If you're really committed to doing it without a
    loop, then something like this will do it:

    #include <algorithm>
    #include <fstream>
    #include <iostream>
    #include <iterator>

    using namespace std;

    void
    append_file_to_stream(ofstream& out, const char *infile)
    {
    ifstream in(infile, ios_base::in | ios_base::binary);
    in.exceptions(ios_base::badbit | ios_base::failbit);

    istreambuf_iterator<char> in_it(in);
    istreambuf_iterator<char> end;
    ostreambuf_iterator<char> out_it(out);

    copy(in_it, end, out_it);
    }

    int main()
    {
    ofstream out("outfile.bin", ios_base::eek:ut | ios_base::binary);
    out.exceptions(ios_base::badbit | ios_base::failbit);

    append_file_to_stream(out, "infile1.bin");
    append_file_to_stream(out, "infile2.bin");
    }


    It's got a certain charm, but I've never really used this
    kind of thing in real code, so YMMV.

    Sean
    , Jan 15, 2009
    #3
    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. Gary Morrison
    Replies:
    5
    Views:
    2,564
    Gary Morrison
    Mar 28, 2006
  2. Unforgiven
    Replies:
    5
    Views:
    497
    Unforgiven
    Oct 16, 2003
  3. sasuke
    Replies:
    5
    Views:
    2,015
    Tom Anderson
    Jul 6, 2008
  4. Jim
    Replies:
    6
    Views:
    731
  5. Rider
    Replies:
    6
    Views:
    177
    Charlton Wilbur
    Jul 26, 2009
Loading...

Share This Page