c++ - 试图输出 exe 文件中的所有内容

标签 c++ ifstream

我正在尝试输出此 .exe 文件的明文内容。它包含明文内容,例如“以这种方式更改代码不会影响生成的优化代码的质量”。微软放入 .exe 文件的所有内容。当我运行以下代码时,我得到 M Z E 的输出,后跟一颗心和一颗钻石。我做错了什么?

ifstream file;
char inputCharacter;    

file.open("test.exe", ios::binary);

while ((inputCharacter = file.get()) != EOF)
{   

    cout << inputCharacter << "\n";     
}


file.close();

最佳答案

我会使用类似 std::isprint 的东西在打印之前确保字符是可打印的,而不是一些奇怪的控制代码。

像这样:

#include <cctype>
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream file("test.exe", std::ios::binary);

    char c;
    while(file.get(c)) // don't loop on EOF
    {
        if(std::isprint(c)) // check if is printable
            std::cout << c;
    }
}

关于c++ - 试图输出 exe 文件中的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33113187/

相关文章:

c++ - 使用 rcpp/rinside 包时如何将 dll 库包含在 makefile 中

C++ 从文本文件中读取并计算每行的总数 :

c++ - (c++) 使用 ifstream 将 .dat 文件读取为十六进制

c++ - 读取文件内容时未定义的字符,文件末尾没有新行

c++ - 链接错误 - 功能二叉树,派生类

Android 手机的 Java 编译器

c++ - 网络浏览器中定义的 zlib gzip 无效响应 (c++)

c++ - 有效地传递对象?

c++ - 通过函数传递 ifstream 文件时出错

c++ - 为什么我无法输入文件中的整数?