我想按字節複製一個exe文件。我比較了2的十六進制文件,它們是完全不同的。好像還沒有得到加載某些值..有人能告訴我爲什麼複製這樣的文件不起作用嗎?
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(){
ifstream fin("file.exe", ifstream::binary);
vector<char> buffer(1, 0);
ofstream newfile;
newfile.open("newfile.exe", ios::binary);
while (fin.read(buffer.data(), buffer.size())){
streamsize s = fin.gcount();
for (int i = 0; i < buffer.size(); i++){
if (buffer[i] != EOF){
newfile << buffer[i];
cout << buffer[i] << endl;
} else {
break;
}
}
}
}
您是否需要streamsize s = fin.gcount()語句?無論如何,你都不會使用它的結果。 –
該向量是爲了防止我一次抓取多個字節。我比較EOF因爲迄今爲止工作。 – Jlegend
最明顯的問題是'buffer [i]!= EOF'。由於'buffer [i]'包含一個字符,'EOF'不是一個字符,所以這個比較沒有意義。 –