1

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

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

Comments

3

You can use fflush inside the loop to enforce write-back to the file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.