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: enter image description here

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.

  • This is my first question. I ask you to excuse me - DP0
  • in_file would also need to be checked for NULL before fclose . - αλεχολυτ
  • The second file does not cause problems. The problem sits clearly in the first file. - DP0
  • one
    The problem can manifest itself only in the complex, and its source can be in_file . Add the in_file check to NULL and check the code again. "some phrase" may well fail to be output due to buffering, although the printf code printf already been executed. - αλεχολυτ

2 answers 2

You do not have to verify that fopen("blabla.txt","w") did not return NULL .

If this happens, then NULL is committed to fclose .
This leads to unspecified behavior, and the program most likely falls.

  • Is not a fact. When the flag "w" file is created if it does not exist. And, here, the second fclose can return NULL. - Hermann Zheboldov
  • I tried to do a check on NULL. did not help. fopen returns a working descriptor. Duplicates appear, as a rule, after the phrase "some phrase \ n" - DP0
  • @ DP0 add this check to the code in question - Abyx
  • @ DP0, are you checking both descriptors? - Hermann Zheboldov
  • After all, an error can occur both at the fopen stage and at the fclose stage, unless there is no - DP0

If you scroll the scroll bar up, then down again, all duplicates disappear (overwrite). I think there is a Windows 8 error here, but this is just a guess. I do not have the opportunity to check on earlier versions of Windows.

The question can be considered closed. Thank you all for participating.

  • Add the output of any message at the beginning of the program (to understand that it has started) and redirect the output to a file. If after the launch message there will always be "some phrase", then the problem is with the windows console. - insolor
  • @insolor. "Add the output of any message at the beginning of the program (to understand that it has started) and redirect the output to a file." It is not clear, you need to redirect the output of the entire program, or only the first message. "If after the launch message there will always be some phrase" ... ". It is not clear what kind of launch message if we redirect them to a file. I can not catch the idea that you want to convey to me. - DP0
  • if your program is launched via a bat-script, then simply redirect its output to a file via > . I suggested adding a "something" output to the beginning in order to understand that the program started. If you run a bat script, then it is enough not to disable the echo output. - insolor
  • @insolor, it is now clear what was meant, but today, oddly enough, I could not repeat this mistake. the system suddenly began to work hard. as soon as this error again makes itself felt, i will do the bat-file and redirect. Perhaps one of the programs that I use climbs into the address space cmd and spoils it, maybe something else. you can think of anything. - DP0
  • Sometimes it happens, one of my programs displays 11 lines in the console, and I sit and think: "why are there 11 of them? should be 10". then I slightly scroll the console up, then down and see my 10 lines. as a result, time is wasted in vain on the fact that I was looking for an error which is not. - DP0