I have 2 functions. The first function is opening a file in write mode and writing some contents to it and then closing it.
FILE *fp = fopen("file.txt", "w");
//writing itnot file using fwrite
fclose(fp);
The second function opens the file in read mode, parses the content and then closes the file.
FILE *fp = fopen("file.txt", "r");
//parsing logic
fclose(fp);
In main
, I am calling function1
and function2
sequentially.
int main()
{
function1();
function2();
return 1;
}
Sometimes, function1
fopen
fails with error number 13 i.e. Permission Denied. I am observing this only sometimes. I introduced a sleep
in function1
after fclose
for 2 seconds and it started working fine without any issues.
So I am suspecting file is not immediately released after fclose
. Sleep is not the right solution. Can anyone suggest how to resolve this problem? The example I have given here is a use case and the actual code is running in a thread environment.