Why the no. of lines in data files are different when they are supposed to be same?

Discussion in 'Mac Programming' started by hajime, Sep 4, 2011.

  1. hajime macrumors 68030

    Joined:
    Jul 23, 2007
    #1
    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.
     
  2. willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #2
    It makes it much easier for other people to help if you post code. At least post the while loop so that other eyes can take a look at it.

    You could try putting a counter for each output file in the while loop. Whenever the fprintf is called for an output file, increment that counter and print out the results. That way you'll be able to see if the files really are staying synced or not.
     
  3. hajime thread starter macrumors 68030

    Joined:
    Jul 23, 2007
    #3
    Thanks for the suggestion. I have some related questions.

    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. Could my problem be caused by the fact that I force terminated the program without using fclose to close the files?

    4. It is probably unlikely but I am asking anywhere. If I perform some computationally intensive stuff under Mac OS, is there a possibility that the OS is not able to catch up and fails to write all the data to the files at the same time?

    I noticed that in another project, whenever I opened a large data file under EXCEL, the program halted. I had to force quit after waiting for 2 or 3 hours for the program to open the file.
     
  4. willieva macrumors 6502

    Joined:
    Mar 12, 2010
    #4
    1. I don't know if you're using fflush correctly, you didn't post your code. The first thing I would check for is to see if the argument to each fflush matched the fprintf from the previous line.

    2. No.

    3. Maybe, but not likely.

    4. Finding a bug in the OS or compiler is much rarer than finding a bug in your own code.

    Seriously, it's really hard to debug code without seeing it. Please post it.
     

Share This Page