int ReadFile(const char *filePath, char **content, int &nFileLen)
{
FILE *pF = NULL;
pF = fopen(filePath, "r");
if (pF == NULL) {
return -1;
}
fseek(pF, 0, SEEK_END); // 设置current position为相对于文件末尾偏移0字节,即文件末尾
nFileLen = ftell(pF); // 获取current position,以字节为单位,因为当前已经处于文件末尾了,所有ftell的返回值就是文件的大小
rewind(pF); // 设置current position为文件开头
char *szBuffer = (char*)memalign(64, nFileLen); // 64字节对齐分配内存,也可以使用malloc
if (!szBuffer)
{
fclose(pF);
return -1;
}
nFileLen = fread(szBuffer, sizeof(char), nFileLen, pF);
fclose(pF);
*content = szBuffer;
return 0;
}