這篇博客是對上一篇博客(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文件的運行結果:
復制成功