-
September 4th, 2011, 08:15 PM
#1
Why the number of lines in data files are different when they are supposed to be same
Hello. I have a C program which does simulation and saves the data into five different data files at each iteration of a while loop. However, when I stopped the program, I noticed that the files have different number of lines while they are supposed to be the same. This happens all the time. I can imagine that there may be a line difference if I terminated the program in the middle of the loop. However, it seems that the longer I let the program runs before force termination, the more the difference in the number of lines. It looks like that the files are divided into two groups, files1, files3, files4 have X lines while files 2 and files5 have Y lines.
For example, three of the files have 12345 lines while the other two have 12912 lines. Anybody knows what is going on and how to resolve the issue? I read that sometimes the data are in the buffer before being written to a file. I already added a fflush command immediate behind each fprintf statement. Do I need any other command besides fflush?
e.g. fprintf(outfile3, "%g %g %g \n", a, b, c);
fflush(outfile3);
:
:
fprintf(outfile4, "%d %g %d \n", d, e, f);
fflush(outfile4);
As I force terminate the program each time, I do not fclose the files. Is this the problem?
I am running the program under Mac OS X Snow Leopard. I use gcc to compile.
Thanks.
Last edited by hajimeml; September 4th, 2011 at 08:28 PM.
-
September 4th, 2011, 08:47 PM
#2
Re: Why the number of lines in data files are different when they are supposed to be
Originally Posted by hajimeml
Hello. I have a C program which does simulation and saves the data into five different data files at each iteration of a while loop. However, when I stopped the program, I noticed that the files have different number of lines while they are supposed to be the same. This happens all the time. I can imagine that there may be a line difference if I terminated the program in the middle of the loop. However, it seems that the longer I let the program runs before force termination, the more the difference in the number of lines. It looks like that the files are divided into two groups, files1, files3, files4 have X lines while files 2 and files5 have Y lines.
For example, three of the files have 12345 lines while the other two have 12912 lines. Anybody knows what is going on and how to resolve the issue? I read that sometimes the data are in the buffer before being written to a file. I already added a fflush command immediate behind each fprintf statement. Do I need any other command besides fflush?
e.g. fprintf(outfile3, "%g %g %g \n", a, b, c);
fflush(outfile3);
:
:
fprintf(outfile4, "%d %g %d \n", d, e, f);
fflush(outfile4);
As I force terminate the program each time, I do not fclose the files. Is this the problem?
I am running the program under Mac OS X Snow Leopard. I use gcc to compile.
Thanks.
Seriously, do you think anyone can help if they don't have your entire program? Maybe your program has a bug and you need to do as any programmer would do -- debug your program.
Regards,
Paul McKenzie
-
September 4th, 2011, 08:55 PM
#3
Re: Why the number of lines in data files are different when they are supposed to be
Originally Posted by Paul McKenzie
Seriously, do you think anyone can help if they don't have your entire program? Maybe your program has a bug and you need to do as any programmer would do -- debug your program.
Regards,
Paul McKenzie
Sorry for the misunderstanding. Perhaps I should frame my questions the other way. I want to know:
1. Am I using fflush correctly?
2. Do I have to use other command(s) besides fflush?
I recall reading somewhere that using fflush alone is not enough. I need to use one more
command. I cannot find the information right now.
3. Is my problem caused by the fact that I force terminated the program without using fclose to close the files.
Thanks.
-
September 4th, 2011, 10:42 PM
#4
Re: Why the number of lines in data files are different when they are supposed to be
Originally Posted by hajimeml
3. Is my problem caused by the fact that I force terminated the program without using fclose to close the files.
Maybe. So let's say you call fclose() and you still see that the files are different sizes. Then what?
We know nothing about your program to determine whether it has bugs that go beyond what you've mentioned so far.
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|