0

I have a C program that is trying to record something called an "avalanche size". The criteria for recording this is if the "delta_energy" which is generated by the program is less than equal to zero then I can increment my avalanche size by "*avalanche_size = *avalanche_size + 1;" and if the delta_energy is not less than equal to zero then it continues running the loop without incrementing the avalanche size.

So what I want to do is to write the delta energy WHEN THE AVALANCHE SIZE IS INCREMENTED (not otherwise) to a file called delta_energies_GSA as shown in the code below.

But I what I find to happen is that if I put the fprintf statement inside the if{ } where the avalanche size is incremented for sure, everytime it does one iteration, it over-writes all the entries in the file. So in the end I end up with the file containing only the delta energies for one of the iterations. If I take the fprintf statemenet and put it outside the bracket, it records everything but it also gives me the delta energies for when the avalanche size is not incremented and I don't want those.

I thought about doing maybe a condition like "if the avalanche size is bigger than the previous avalanche size then fprintf the delta energy" ... but I'm not sure how to do this since avalanche size is just an integer not a vector..

Any help would be really appreciated! Thank you

for (k = 0; k < n_nodes; k++)
     {
       if (delta_energy <= 0.0)
        {
            stored_state[i] = new_state;
            *avalanche_size = *avalanche_size + 1;
            printf("\n\n For k = %d: ",k);
            printf("\n\n This is the delta energy with GSA for %d avalanche size:%f", *avalanche_size, delta_energy);

            fprintf(delta_energies_GSA,"\n %d\t %d\t %f \n",k, *avalanche_size, delta_energy);

        }

I haven't shown the full code because its a very large function of a very large program. I have also been very careful about when I open and close the file. The file is opened right at the beginning of the function after I have declared my variables. And I close the file right before the function ends.

This is how the file is opened:

{
    double  d_energy, q_A_minus_1, one_over_q_A_minus_1, prob_term;
    neighbor_inf    *np;

extern  int generate_new_state();

FILE *delta_energies_GSA;


delta_energies_GSA = fopen("delta_energies_GSA.dat", "w");
if (delta_energies_GSA == NULL) 
{
    printf("I couldn't open delta_energies_GSA.dat for writing.\n");
    exit(0);
}

Right after declaring my variables and it is closed before the function ends:

    fclose(delta_energies_GSA);
return(stored_state);

}    /* end recover_stored_patterns_GSA() */
Maheen Siddiqui
  • 185
  • 4
  • 13
  • As for the file overwriting, when, where and *how* do you open the file? – Some programmer dude Aug 10 '13 at 12:10
  • Sorry I have corrected that, it was just a mistake on my part, I meant less than equal to zero. Thanks – Maheen Siddiqui Aug 10 '13 at 12:11
  • I have shown when and how I open the file now as well. I just dont want to paste the whole function as its quite big. – Maheen Siddiqui Aug 10 '13 at 12:17
  • 2
    See the docs for [`fopen()`](http://en.cppreference.com/w/c/io/fopen), specifically the note on the "w" open-mode and what it does when the file already exists. (look for the words "destroy contents"). – WhozCraig Aug 10 '13 at 12:17
  • Oh! I've changed it to "a+" instead of "w" and now its writing all of them. Thanks a lot! I'm just a bit curious though.. I have this fprintf in lots of functions with while and for loops running but it always writes all the entries and this over-writing thing never happened except now.. But my problem is sorted now, many thanks! – Maheen Siddiqui Aug 10 '13 at 12:26

1 Answers1

1

The fprintf() does exactly what you want to do, append a string to a file, I don't see anything wrong here with your code if the fopen() is outside the for loop (in this case use "w+" with fopen, for append) and there aren't seek to 0.

EDIT My wrong, not "w+" but "a" for append (and if you don't need to also read the file).

The wrong behavior that you must investigate is "why the fprintf replace the whole file". Try this simple test.

#include <stdio.h>

int main(int argc, char **argv) {
    FILE *f = fopen("test", "w");
    if (f) {
            int i;
            for (i=0; i<100; i++) {
                    if (i % 3)
                            fprintf(f, "%d\n", i); 
            }
            fclose(f);
    }
    return 0;
}
Alex
  • 3,264
  • 1
  • 25
  • 40