1,使用fopen(FileName, “r”) r的方式读取文件到内存,此时就可能存在问题了,r以只读方式,有时候不表示字符的有可能就不读了,那么内存中就不会是完整的原始文件。所以此时要采用rb,二进制读取的方式。
bool ReadFileToMem(char* FileName, char**buf)
{
FILE* f;
f = fopen(FileName, “rb”);
if (f == NULL)
{
goto failed;
}
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size == 0)
{
goto failed;
}
buf = (char)malloc(size);
if (*buf == NULL)
{
goto failed;
}
int ret = fread(*buf, 1, size, f);
if (ret == 0)
{
goto failed;
}
printf(“read file: %s success\n”, FileName);
fclose(f);
return true;
failed:
fclose(f);
return false;
}