这个程序为什么不能正确读取 hex 文件?
#include <stdio.h>
#include <stdlib.h>
char *buffer;
unsigned int fileLen;
void ReadFile(char *name);
void ReadFile(char *name)
{
FILE *file;
file = fopen(name, "rb");
if (!file)
{
fprintf(stderr, "Unable to open file %s", name);
return;
}
//Getting file length
fseek(file, 0, SEEK_END);
fileLen=ftell(file);
fseek(file, 0, SEEK_SET);
//Allocating memory
buffer=(char *)malloc(fileLen+1);
if (!buffer)
{
fprintf(stderr, "Mem error!");
fclose(file);
return;
}
fread(buffer, fileLen, 1, file);
fclose(file);
}
int main()
{
var32 code;
char filename[20];
printf("Enter the file name: ");
scanf("%s", &filename);
ReadFile(filename);
printf("FIle contents: %x\n",buffer);
}
如果我打印一个巨大的十六进制文件,它只会打印 5 到 6 位数字。