我正在尝试使用fread读取一个空文件。在此之前,我创建了一个块大小为4096且有40个块的文件。目前我知道这些块是“空的”,但是如果按照下面我的代码读取文件,我无法确定它是否为空。我的意思是我期望nread是NULL或类似的东西。你知道我需要将nread与什么进行比较吗?谢谢!
int test()
{
char buf[4096];
FILE *file;
size_t nread;
file = fopen("out/abc.store", "r");
if (file) {
while ((nread = fread(buf, 1, sizeof buf, file)) > 0)
fwrite(buf, 1, nread, stdout);
if (ferror(file)) {
/*error handling*/
}
fclose(file);
}
编辑:
我是这样创建文件的:
char *content=(char*)malloc(uintBlockSize*uintBlockCount);
memset(content,0,uintBlockSize*uintBlockCount);
...
while (i!=0)
{
check=fwrite(content,uintBlockSize, 1, storeFile);
if (check!=1)
return 1;
i--;
}
malloc()
的返回值强制转换。 - unwind