Scorpious
Scorpious

Reputation: 133

I am not able to copy the entire binary file

    ifstream ifile("/home/zuma/xps.mp3", ios::binary | ios::in);
    ofstream ofile("/home/zuma/xxx.mp3", ios::binary | ios::out);

    copy(istream_iterator<unsigned char>(ifile), istream_iterator<unsigned char>(), ostream_iterator<unsigned char>(ofile));

    ifile.close();
    ofile.close();

The new file created has lesser bytes than the original one & the files do not match

Upvotes: 2

Views: 327

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

istream_iterator uses operator>>, which is whitespace delimited (and no, opening the file in binary mode does not change this behavior). Use istreambuf_iterator instead.

istreambuf_iterator<char> in1(ifile), in2;
ostreambuf_iterator<char> out(ofile);
copy(in1, in2, out);

Or, as ildjarn mentioned, you can copy the whole file with much less typing:

ofile << ifile.rdbuf();

Upvotes: 5

Related Questions