我正在制作一个小程序,如下所示:
我的PC剩余的磁盘空间约为500 MB。在
如何学习磁盘空间是否足够处理正确?
我正在制作一个小程序,如下所示:
void reserve_file_space(char* file_path, size_t amount)
{
FILE* fp = fopen(file_path, "w+b");
if(!fp)
{
printf("could not create a new file\n");
return;
}
int fseek_ret = fseek(fp, amount, SEEK_SET);
if(fseek_ret != 0)
{
printf("could not seek to the desired position\n");
fclose(fp);
return;
}
char garbage = 1;
size_t ret = fwrite(&garbage, 1, 1, fp);
if(ret != 1)
{
printf("could not write the garbage character\n");
}
fclose(fp);
}
int main(int argc, char* argv[])
{
reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 30);
return 0;
}
我的PC剩余的磁盘空间约为500 MB。在
main()
中,如果我调用reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 28 /*256 MB*/);
,它将创建一个精确大小为256 MB的文件。但是,如果我调用reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 30 /*1 GB*/);
则会生成大小为0的输出文件,并且不打印任何错误通知。如何学习磁盘空间是否足够处理正确?
fseek
和甚至fclose
的返回值。 - kaylum