I am making a small program as follows:
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;
}
The free disk space on my PC is around 500 MBs. In the main()
if I invokes reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 28 /*256 MB*/);
it outputs a file created with exact size of 256 MB. However if I invokes reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 30 /*1 GB*/);
it produces the output file with size of 0 and without printing out any error notice.
How can I learn if the disk space is sufficient to handle correctly?