Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to output everything inside an exe file

Tags:

c++

ifstream

I'm trying to output the plaintext contents of this .exe file. It's got plaintext stuff in it like "Changing the code in this way will not affect the quality of the resulting optimized code." all the stuff microsoft puts into .exe files. When I run the following code I get the output of M Z E followed by a heart and a diamond. What am I doing wrong?

ifstream file;
char inputCharacter;    

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

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

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


file.close();
like image 248
Jrow Avatar asked Dec 06 '25 01:12

Jrow


1 Answers

I would use something like std::isprint to make sure the character is printable and not some weird control code before printing it.

Something like this:

#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;
    }
}
like image 121
Galik Avatar answered Dec 08 '25 13:12

Galik