سؤال

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() */
هل كانت مفيدة؟

المحلول

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top