Not writing to file?

C

Cliff

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
 
B

Burne C

Cliff said:
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 *).
 
R

Richard Bos

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
 
D

Dan Pop

In said:
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top