Not writing to file?

  • Follow


Hi all. I'm working on a program that writes a line of text to a file
during each iteration of a for loop. This for loop may run for quite a
while and sometimes I would like to stop it ("Ctrl-C") before the
program finish executing. My problem is the data does not get written
to file until the entire program executes.

Here's the relevant code: 

for(n=0;n<iter;n++){
	sprintf(line,"%d\t%e\t%e\t%e\t%e\t%e\t%e\t%d\n", n2, startV, stopV,
mm.maxV, mm.maxI, mm.minV, mm.minI, DPNT);
        fprintf(summary_file, line);
}

Any help with the code or explanation as to why this happens would be
appreciated.

Cliff
0
Reply hondajacka (1) 7/25/2003 6:31:35 AM

"Cliff" <hondajacka@hotmail.com> wrote in message
news:5f511ab5.0307242231.50c6bb73@posting.google.com...
> Hi all. I'm working on a program that writes a line of text to a file
> during each iteration of a for loop. This for loop may run for quite a
> while and sometimes I would like to stop it ("Ctrl-C") before the
> program finish executing. My problem is the data does not get written
> to file until the entire program executes.
>
> Here's the relevant code:
>
> for(n=0;n<iter;n++){
> sprintf(line,"%d\t%e\t%e\t%e\t%e\t%e\t%e\t%d\n", n2, startV, stopV,
> mm.maxV, mm.maxI, mm.minV, mm.minI, DPNT);
>         fprintf(summary_file, line);
> }
>
> Any help with the code or explanation as to why this happens would be
> appreciated.
>
> Cliff

Use fflush( FILE *).

--
BC



0
Reply Burne 7/25/2003 7:03:36 AM


hondajacka@hotmail.com (Cliff) wrote:

> Hi all. I'm working on a program that writes a line of text to a file
> during each iteration of a for loop. This for loop may run for quite a
> while and sometimes I would like to stop it ("Ctrl-C") before the
> program finish executing. My problem is the data does not get written
> to file until the entire program executes.

Then you probably want the fflush() function:

               #include <stdio.h>
               int fflush(FILE *stream);

       Description

       [#2] If stream points to  an  output  stream  or  an  update
       stream in which the most recent operation was not input, the
       fflush function causes any unwritten data for that stream to
       be  delivered  to  the host environment to be written to the
       file; otherwise, the behavior is undefined.

(From n869.txt, the last public draft of the Standard.)

> Here's the relevant code: 
> 
> for(n=0;n<iter;n++){
> 	sprintf(line,"%d\t%e\t%e\t%e\t%e\t%e\t%e\t%d\n", n2, startV, stopV,
> mm.maxV, mm.maxI, mm.minV, mm.minI, DPNT);
>         fprintf(summary_file, line);

For example, you could put

        fflush(summary_file);

here. Or do it only every 10 iterations, for a bit more efficiency.

> }
> 
> Any help with the code or explanation as to why this happens would be
> appreciated.

Buffering. It's usually more efficient to write a large block of a file
in one go rather than several small bits one after another, so the
output to your file is saved up and only passed to the OS when the
buffer is full or you close the file - or you call fflush().

Note that fflush() does not guarantee that your data does get written to
disk, since the OS can do its own buffering and the C Standard has no
control over what OSes do, of course. However, it does guarantee that
the data has left the program buffer and is safely in the hands of the
OS, which makes it rather more likely that it gets written even when the
program is aborted.

Richard
0
Reply rlb (4118) 7/25/2003 7:49:41 AM

In <5f511ab5.0307242231.50c6bb73@posting.google.com> hondajacka@hotmail.com (Cliff) writes:

>Hi all. I'm working on a program that writes a line of text to a file
>during each iteration of a for loop. This for loop may run for quite a
>while and sometimes I would like to stop it ("Ctrl-C") before the
>program finish executing. My problem is the data does not get written
>to file until the entire program executes.
>
>Here's the relevant code: 
>
>for(n=0;n<iter;n++){
>	sprintf(line,"%d\t%e\t%e\t%e\t%e\t%e\t%e\t%d\n", n2, startV, stopV,
>mm.maxV, mm.maxI, mm.minV, mm.minI, DPNT);
>        fprintf(summary_file, line);
>}
>
>Any help with the code or explanation as to why this happens would be
>appreciated.

Most likely because Ctrl-C doesn't cause normal program termination and
the buffers of your streams are not flushed.

You have three options:

1. Call fflush(summary_file) after each fprintf(summary_file,...) call.

2. Disable the buffering on summary_file with setbuf(summary_file, NULL)
   right after opening it or set it to line buffering with a corresponding
   setvbuf() call.

3. Write a signal handler for the SIGINT signal.  When the program 
   "catches" the Ctrl-C, it terminates in a normal way, either by calling
   exit() or by returning from main().  This will cause *all* your streams
   to be properly flushed and closed.

Dan
-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
0
Reply Dan.Pop (3615) 7/25/2003 10:06:07 AM

3 Replies
40 Views

(page loaded in 0.069 seconds)


Reply: