Outputting text to a file

R

Robert

Hi,

I'm creating simple logging function in my server application but i've come
to a little problem. I use fopen and fprintf to open and print to that file
and after the application quits fclose ofcourse but the data get's only
written when i use fclose, i need something so it gets written to the file
directly because otherwise the logging won't have much effect because i want
to run the program for long times. Is there any function to flush the buffer
to the file?

Thanks in Advance,

Robert Mens
 
J

Joona I Palaste

Robert said:
I'm creating simple logging function in my server application but i've come
to a little problem. I use fopen and fprintf to open and print to that file
and after the application quits fclose ofcourse but the data get's only
written when i use fclose, i need something so it gets written to the file
directly because otherwise the logging won't have much effect because i want
to run the program for long times. Is there any function to flush the buffer
to the file?

Try fflush().
 
M

Martin Ambuhl

Robert said:
Hi,

I'm creating simple logging function in my server application but i've come
to a little problem. I use fopen and fprintf to open and print to that file
and after the application quits fclose ofcourse but the data get's only
written when i use fclose, i need something so it gets written to the file
directly because otherwise the logging won't have much effect because i want
to run the program for long times. Is there any function to flush the buffer
to the file?

Surprisingly, it's called fflush().
 
D

Dan Pop

In said:
Try fflush().

If it doesn't have the desired effect (the output is still buffered by
the OS, even if it left your program), close the file after logging each
item and reopen it in append mode before logging a new item. With some
network file systems this may be your only chance of forcing the output to
its final destination (short of using non-standard library calls).

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top