C ++:提取运算符(>>)跳过一些字节

时间:2018-11-18 17:11:04

标签: c++ file file-io

我正在尝试编写一个程序,该程序逐字节读取文件(在这种情况下为可执行文件),然后将其写入新文件,然后该文件应该相同。

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();

在此示例中,out文件最终缩短了284个字节。 Here是十六进制查看器中前800个字节的比较。输入文件'09'的第70个字节被跳过。我可能缺少明显的东西,但是我无法掌握。

预先感谢

1 个答案:

答案 0 :(得分:0)

为避免跳过空格,请尝试putget处理未格式化的流。

while (infile.get(x)) {
    outfile.put(x);
}