Reputation: 416
I have a parent process and a child process (children are created using fork) some where in the parent process this code is defined :
FILE* pfile = fopen("log.txt","w");
while (1) {
serve child requests
fprintf (pfile,"some data\n");
}
fclose (pfile);
the problem is the last line of the code never gets executed because the infinite while loop does not terminate (this is how the program should act) .. so the file will never be closed and consecutively the written data wont be saved into the file.
How can i solve this problem ?
Any help would be greatly appreciated , Thanks
Upvotes: 1
Views: 90
Reputation: 4380
The data is saved when the buffer is full. In the meantime, you can also force the file commit with a fflush()
-- the file itself will physically close when the app or while loop terminates.
FILE* pfile = fopen("log.txt","w");
while (1)
{
serve child requests
fprintf (pfile,"some data\n");
fflush(pfile);
}
fclose (pfile);
Upvotes: 5
Reputation: 5307
You can use fflush
inside the loop to enforce write-back to the file.
Upvotes: 3