gpt4 book ai didi

c++ - 读取可执行文件的节数失败

转载 作者:行者123 更新时间:2023-11-27 23:18:14 25 4
gpt4 key购买 nike

不知何故,以下代码输出“Number of sections: 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;
}

最佳答案

istream_iterator使用 operator<<这是unsuitable for binary data

使用 istream::read反而。

您可以获取文件大小,通过搜索到末尾,然后调用 tellg .相应地调整 vector 的大小并直接读取它。

关于c++ - 读取可执行文件的节数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15088977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com