How to do fprintf () immediately
One way to write to a file is with fprintf()
. However, this function does not write the results to a file immediately . Most likely, it writes everything at once when the program ends or ends.
My question is this: I have a program that takes a very long time (4-5 hours for a large dataset). During this time, I want to see interim results, so I don't have to wait 5 hours. My university uses Sun Grid Engine
to submit an assignment. As you know, you need to wait for your work to finish to see your final results. Thus, I want to be able to write intermediate results to a text file and see updated results as the program is processed (similar if I use printf
).
How can I change fprintf()
to write whatever I want directly to the target file?
source to share
You can use the function fflush
after every write to flush the output buffer to disk.
fprintf(fileptr, "writing to file\n");
fflush(fileptr);
If you are on a POSIX system (e.g. Linux, BSD, etc.) and you really want the file to be written to disk, that is, you want to flush the kernel buffers as well as the user environment buffers, also use fsync
:
fsync(fileno(fileptr));
But it fflush
should be enough. Don't worry fsync
if you don't find what you need.
source to share
fflush
This works for FILE *. This seems more appropriate for your case. Note that fflush (NULL) will update all open files / streams and I will be CPU intensive. You can use / avoid fflush (NULL) for performance reasons.
FSYNC
This is working on an int descriptor. It not only updates the file / stream but also the metadata. It can work even in cases of system crash / reboot. You can check the man page for more details.
I personally use fflush and it works great for me (on Ubuntu / Linux).
source to share