【问题标题】:Trying to output everything inside an exe file尝试输出exe文件中的所有内容
【发布时间】:2015-10-13 21:45:05
【问题描述】:

我正在尝试输出此 .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();

【问题讨论】:

    标签: c++ ifstream


    【解决方案1】:

    我会使用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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      您已经以二进制形式打开了流,这对预期目的是有益的。但是,您按原样打印每个二进制数据:其中一些字符不可打印,从而产生奇怪的输出。

      可能的解决方案:

      如果您想打印 exe 的内容,您将获得比可打印字符更多的不可打印字符。因此,一种方法可能是打印十六进制值:

      while ( file.get(inputCharacter ) )
      {   
          cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff) << "\n";     
      }
      

      或者您可以使用显示十六进制值的调试器方法,然后显示可打印的字符或“。”如果没有:

      while (file.get(inputCharacter)) {
          cout << setw(2) << setfill('0') << hex << (int)(inputCharacter&0xff)<<" ";
          if (isprint(inputCharacter & 0xff))
              cout << inputCharacter << "\n";
          else cout << ".\n";
      }
      

      好吧,为了人机工程学,如果 exe 文件包含任何真正的 exe,您最好选择在每行显示几个字符 ;-)

      【讨论】:

        【解决方案3】:

        二进制文件是字节的集合。字节的值范围为 0..255。可以安全“打印”的可打印字符的范围要窄得多。假设最基本的 ASCII 编码

        • 32..63
        • 64..95
        • 96..126
        • 如果您的代码页有,可能还有一些高于 128

        ascii table

        超出该范围的每个字符至少可以:

        • 打印为不可见
        • 打印为一些奇怪的垃圾
        • 实际上是一个会改变终端设置的控制字符

        一些终端支持“文本结束”字符,之后会停止打印任何文本。也许你击中了。

        我想说,如果您只对文本感兴趣,那么只打印那些可打印的内容而忽略其他内容。或者,如果你想要所有东西,那么也许可以用十六进制形式写出来?

        【讨论】:

          【解决方案4】:

          这行得通:

          ifstream file;
          char inputCharacter;
          string Result;
          
          file.open("test.exe", ios::binary);
          
          while (file.get(inputCharacter))
          {       
              if ((inputCharacter > 31) && (inputCharacter < 127))
                  Result += inputCharacter;       
          }
          
          cout << Result << endl;
          cout << "These are the ascii characters in the exe file" << endl;
          file.close();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-11-05
            • 2022-06-19
            • 2021-09-03
            • 1970-01-01
            • 2015-10-02
            • 1970-01-01
            • 2023-03-21
            相关资源
            最近更新 更多