如何在c中阅读.exe

我正在制作一个小型压缩程序的小项目。 为此,我想读取一个文件,比如一个.exe,然后用char解析它char并使用一些简单的字典算法来加密它。

为了阅读文件我只是使用一个简单的代码我发现:

char *readFile(char *fileName) { FILE *file; char *code = malloc(10000* sizeof(char)); file = fopen(fileName, "rb"); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } 

我的问题是,读取.exe或任何文件似乎是不可能的。 在制作“代码”的printf()时,没有任何内容被写入。

我能做什么?

@BLUEPIXY很好地识别了代码错误。 见下文。 你也返回字符串的结尾,可能想要返回开头。

 do { // *code++ = (char)fgetc(file); *code = (char)fgetc(file); // } while(*code != EOF); } while(*code++ != EOF); 

让你开始阅读任何文件的东西。

 #include  #include  void readFile(const char *fileName) { FILE *file; file = fopen(fileName, "rb"); if (file != NULL) { int ch; while ((ch = fgetc(file)) != EOF) { if (isprint(ch)) { printf("%c", ch); } else { printf("'%02X'", ch); if (ch == '\n') { fputs("\n", stdout); } } fclose(file); } } 

当读取二进制文件char-by-char时,代码通常接收0到255和EOF,257个不同的值。