When using the following code:
#include <stdio.h> #include <stdlib.h> int main ( int argc, char *argv[] ) { FILE *blabla = fopen("blabla.txt","w"); if (blabla == NULL){ printf("aaaa\n"); exit(1); } if (fclose(blabla) == EOF){ printf("bbbb\n"); exit(1); } FILE *in_file = fopen ( "log.txt", "r" ); printf("some phrase\n"); fclose ( in_file ); return 0; }
the program starts to behave incorrectly after approximately 40th call:
As seen above, the program duplicates command line prompts. Sometimes instead of duplicate invitations appear incomprehensible letters and numbers. Sometimes duplicates are visible after the second or third launch. fopen and fclose always work without errors.
But if you comment out the instructions of the first file descriptor:
#include <stdio.h> #include <stdlib.h> int main ( int argc, char *argv[] ) { //FILE *blabla = fopen("blabla.txt","w"); //if (blabla == NULL){ // printf("aaaa\n"); // exit(1); //} //if (fclose(blabla) == EOF){ // printf("bbbb\n"); // exit(1); //} FILE *in_file = fopen ( "log.txt", "r" ); printf("some phrase\n"); fclose ( in_file ); return 0; }
The problem is not observed. I use MinGW GCC version 4.8.1 on Windows 8. The program was assembled with the command:
gcc -std=gnu11 -o main2 main2.c
Thanks for your support.
in_file
would also need to be checked forNULL
beforefclose
. - αλεχολυτin_file
. Add thein_file
check toNULL
and check the code again. "some phrase" may well fail to be output due to buffering, although theprintf
codeprintf
already been executed. - αλεχολυτ