473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Not writing to file?

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
Nov 13 '05 #1
3 8725

"Cliff" <ho********@hotmail.com> wrote in message
news:5f**************************@posting.google.c om...
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

Nov 13 '05 #2
ho********@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
Nov 13 '05 #3
In <5f**************************@posting.google.com > ho********@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: Da*****@ifh.de
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.