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);
Run Code Online (Sandbox Code Playgroud)
我在fread()得到Debug Assertion Failed.为什么?
MSVC2010,Windows-7-32位
您为缓冲区分配了350880个字节,但尝试读取350880个短路.尝试
bufferPointer = malloc(350880 * sizeof *bufferPointer);
Run Code Online (Sandbox Code Playgroud)
(注意,没有必要使用malloc,因为它可以隐藏bug,所以不赞成.)
你还应该检查你的malloc,fopen和fread调用是否有错误.