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文件的運行結果:

 復制成功


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM