Reputation: 133
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
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