[坑]fread的模式

2021/06 23 22:06
    FILE* fp = NULL;
    char* buff = NULL;
    fopen_s(&fp, filename, "r");
    if (fp != NULL)
    {
        fseek(fp, 0, SEEK_END);
        long fileSize = ftell(fp);
        fseek(fp, 0, SEEK_SET);
        buff = (char*)malloc(fileSize + 1);
        memset(buff, 0, fileSize + 1);
        size_t rdsz = fread(buff, 1, fileSize, fp);
        fclose(fp);
        if (rdsz != fileSize)
        {
            // 此处读文本文件会报错

必须要使用
fopen_s(&fp, filename, “rb”); 的二进制模式才行

--转载请注明: http://blog.coolcoding.cn/?p=4037