0

不知何故,以下代码输出“部分数:57344”,这显然是不正确的。谁能给我一个提示我做错了什么?

#include <windows.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
#include <iterator>
#include <algorithm>

int main() {
std::string file_path = "C:\\foo.exe";
if(!file_path.size()) {
    cout << "No file was selected" << endl;
    return 1;
}
std::ifstream input( file_path.c_str(), std::ios::binary );
std::vector<unsigned char> buffer((
    std::istream_iterator<unsigned char>(input)), 
    (std::istream_iterator<unsigned char>()));

if(!buffer.size()) {
    cout << "Error reading file" << endl;
    return 1;
}
char * BinFile = (char*)&buffer[0];
IMAGE_DOS_HEADER* DOSHeader = (IMAGE_DOS_HEADER*)BinFile;
IMAGE_NT_HEADERS* NTHeader = (IMAGE_NT_HEADERS*)(BinFile + DOSHeader->e_lfanew);
cout << "Number of sections: " << NTHeader->FileHeader.NumberOfSections << endl;

return 0;
}
4

1 回答 1

4

istream_iteratoroperator<<适合二进制数据的用途

改为使用istream::read

您可以通过搜索到最后,然后调用来获取文件大小tellg。相应地调整矢量大小并直接读取它。

于 2013-02-26T12:25:48.503 回答