test.bin 的大小为 7,01,760 字节。我试图从这个文件中读取日期作为缓冲区 bufferPointer 中的“短”。
short * bufferPointer=NULL;
// ==> ANSWER WAS ADDING: bufferPointer = ( short*)malloc(350880); <==
FILE *fp=fopen(" test.bin","rb");
fread(bufferPointer,sizeof(short),350880 ,fp);
fclose(fp);
我在 fread() 处收到调试断言失败。为什么?
MSVC2010,Windows-7-32 位
最佳答案
您为缓冲区分配了 350880 个字节,但尝试读取 350880 个短裤。尝试
bufferPointer = malloc(350880 * sizeof *bufferPointer);
(请注意,转换 malloc 不是必需的,并且因为它可以隐藏错误而被反对。)
您还应该检查您的 malloc、fopen 和 fread 调用是否有错误。
关于c - fread() 的调试断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13356036/