UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » for flushing output (to file) at runtime

#1 2007-11-21 01:13 PM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: for flushing output (to file) at runtime

Hi all,
all the prints are redirected to logfile, trying to get prints at runtime in logs, but as c library collects sufficient data before flushing to stdout(here  logfile), i suspect by reducing the Buffer size(Linux) , logs could be generated at runtime
i also felt  fflush(NULL) , but i think an additional function call for every printf, which may slow a bit.


appreciate your ideas to flush output to file instantly,

Ex FILE * logfile = fopen(./home/build/logfile);

fprintf(logfile, "Test Output");

Offline

#2 2007-11-21 03:30 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: for flushing output (to file) at runtime

fflush(logFile) is the way to do it. I wouldn't worry about performance unless/until it actually becomes a problem (if you're writing to disk then you're probably going to take a performance hit either way, and AFAIK fflush() merely flushes the data to the filesystem immediately, it doesn't wait for the filesystem to actually write the bytes to disk before returning).

If you do find performance is a problem, you might make your code a bit more clever by having it only call fflush(logFile) when you know you probably won't be writing any more log output for a while (e.g. only at the end of an series of operations).

Offline

#3 2007-11-21 03:59 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: for flushing output (to file) at runtime

If you really want all writes to immediately flush with no buffering, then do
"setbuf (fp, NULL);" or "setvbuf (fp, NULL, _IONBUF, 0);" immediately after fopen()
(or at start-up time, if for stdout)...

Offline

#4 2007-11-22 11:15 AM

santoshkumar
Member
Registered: 2007-08-21
Posts: 53

Re: for flushing output (to file) at runtime

Hi RobSeace  ,
Thank you it works. i prefer trying new things while coding , one happened to  flush outputs.

Offline

  • Index
  • » C
  • » for flushing output (to file) at runtime

Board footer

Powered by FluxBB