C++ 复制二进制文件

这篇博客是对上一篇博客(C++ 文件二进制输入输出)的实践。主要目的是实现对二进制文件的复制。                                                                                                       

源文件是一个叫“helloWorld.exe”的文件,在执行后,会打印一句“Hello World!”

目标文件叫“test.exe”,由“helloWorld.exe”而来。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{


    ifstream fin("helloWorld.exe", ios::binary);
    ofstream fout("test.exe", ios::binary);

    bool bRet = true;

    while(!fin.eof()){
        char szBuf;
        fin.read(&szBuf, sizeof(char));

        if(fin.eof()) break;

        if (fout.bad())
        {
            bRet = false;
            break;
        }
        fout.write(&szBuf, sizeof(char));
    }
    
    fout.close();
    fin.close();

    return 0;
}

两个exe文件的运行结果:

 复制成功

posted @ 2020-05-22 10:42  川尘  阅读(890)  评论(3编辑  收藏  举报
`