I am doing 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 space on my PC is about 500 MB. In main(), if I call reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 28 /*256 MB*/);, it produces a file created with an exact size of 256 MB. However, if I call reserve_file_space("C:\\Users\\SysPart\\Desktop\\test.bin", 1 << 30 /*1 GB*/);, it produces an output file of size 0 and does not produce any error messages.
How do I know if there is enough free disk space?
source
share