我正试图编写一个程序,逐字节读取文件(在本例中为可执行文件(,然后将其写入一个新文件,该文件应该是相同的。
char x;
std::ifstream infile("C:/Users/_user_/Desktop/test.exe", std::ios::in | std::ios::binary);
std::ofstream outfile("C:/Users/_user_/Desktop/out.exe", std::ios::out | std::ios::app | std::ios::binary);
while (infile >> x)
{
outfile << x;
}
outfile.close();
infile.close();
在本例中,输出文件最终缩短了284个字节。以下是十六进制查看器中800个第一个字节的比较。将跳过输入文件的第70个字节"09"。我可能错过了一些显而易见的东西,但我不知道是什么。
提前感谢
要避免跳过空白,请尝试对未格式化的流使用put
和get
。
while (infile.get(x)) {
outfile.put(x);
}