从 exe 文件中读取的整数是多少

What's the integer read from a exe file

本文关键字:整数 多少 读取 exe 文件      更新时间:2023-10-16

正如我们所知,windows上的一个普通应用程序由PE头启动,前两个字符是"MZ"。所以我有一个C++程序来打开一个exe文件并读取它。正如我预测的那样,显示的字符是"MZ"。但要读取的整数是难以理解的,每次都不一样。那么,读取的整数是多少?

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream ifs;
    ifs.open("a.exe", ios::in);
    char c[2];
    ifs >> c[0] >> c[1];
    cout << c[0] << c[1] << endl;
    ifs.close();
    ifs.open("a.exe", ios::in);
    int i;
    ifs >> i;
    cout << i << endl;
    ifs.close();
    return 0;
}

将文件解析为整数的尝试失败,因为文件以整数中不合法的字符开头。这使得i中以前的垃圾仍然存在。从本质上讲,您只是打印一个从未设置过值的变量的值。

尝试更改:

int i;

至:

int i = 42;

看看会发生什么。