我无法复制整个二进制文件

I am not able to copy the entire binary file

本文关键字:二进制文件 复制      更新时间:2023-10-16
    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();

创建的新文件的字节数比原始文件少,文件不匹配

istream_iterator使用operator>>,这是空格分隔的(不,以二进制模式打开文件不会改变此行为)。 请改用istreambuf_iterator

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

或者,正如 ildjarn 提到的,您可以用更少的键入来复制整个文件:

ofile << ifile.rdbuf();