如何在C中读取.exe [英] How to read .exe in c
问题描述
我正在做一个小的压缩程序的小项目.为此,我想读取一个文件,比如说一个.exe文件,然后逐个字符地对其进行解析,并使用一些简单的字典算法对其进行加密.
I'm on a little project of making a little compressor program. For that I want to read a file, say an .exe, and parse it char by char and use some simple dictionary algorithm to encrypt it.
我只是使用发现的简单代码来读取文件:
For reading the file I just thout in using a simple code I found:
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()设为代码"时,不会写入任何内容.
My problem is that it's seems imposible to read an .exe or any file at all. When making a printf() of "code" nothing is writen.
我该怎么办?
推荐答案
@BLUEPIXY可以很好地识别代码错误.请参阅以下内容.另外,您返回字符串的结尾,并且可能想返回开头.
@BLUEPIXY well identified a code error. See following. Also you return the end of the string and likely want to return the beginning.
do {
// *code++ = (char)fgetc(file);
*code = (char)fgetc(file);
// } while(*code != EOF);
} while(*code++ != EOF);
让您开始阅读任何文件的方法.
Something to get you started reading any file.
#include <stdio.h>
#include <ctype.h>
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);
}
}
按字符读取二进制文件时,代码通常接收0到255和EOF以及257个不同的值.
When reading a binary file char-by-char, code typically receives 0 to 255 and EOF, 257 different values.
这篇关于如何在C中读取.exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!