Strange behavior using fclose

102 Views Asked by At

When testing the fclose function, the printf("File \"file2.bin\" inexistent!"); line is not reached. If the fclose(p_file2) line is removed, the message is shown.

#include <stdio.h>

int main()
{
    printf("Hello files");
    FILE *p_file1, *p_file2;
    p_file1 = fopen("file1.bin", "w+b");
    p_file2 = fopen("file2.bin", "r+b");
    if (p_file2 == NULL)
        printf("File \"file2.bin\" inexistent!");
    
    fclose(p_file1);
    fclose(p_file2);

    return 0;
}

Why does that happen?

3

There are 3 best solutions below

6
William Pursell On BEST ANSWER

2 primary issues here. The first is that fclose(NULL) leads to undefined behavior, so there is really no point is speculating about the other point, but it is clearly a source of confusion. printf does not always write any data to the output stream. Instead, the data is buffered and the write may be deferred to a later call to printf or until the program exits (or a call to fwrite or fflush or other conditions). Since your program invoked fclose(NULL), bad things happen and the write that should have been deferred never happens, so you don't see the message.

Try adding a call to fflush immediately after the printf.

0
Jabberwocky On

Only call fclose on valid FILE pointers that have actually been returned by fopen.

Basically you want this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
  printf("Hello files");
  FILE* p_file1, * p_file2;
  p_file1 = fopen("file1.bin", "w+b");
  if (p_file1 == NULL)
  {
    printf("Can't open file.\n");  // add \n
    exit(1);
  }

  p_file2 = fopen("file2.bin", "r+b");
  if (p_file2 == NULL)
  {
    fclose(p_file1); // this can be ommited because files are closed automatically upon exit
    printf("Can't open file.\n");   // add \n
    exit(1);
  }
  
  // here we know for sure that both p_file1 and p_file1 are valid
  fclose(p_file1);
  fclose(p_file2);

  return 0;
2
Harith On
  1. Passing a NULL pointer to fclose() invokes undefined behaviour.¹

As the program exits on fopen() failure, we could have a function like so:

static void open_sesame (const char *restrict stream, const char *restrict mode) 
{
    if (!fopen (stream, mode)) {
        perror ("fopen():");
        exit (EXIT_FAILURE);
    }
}

Now your code simplifies to:

FILE *fp1 = open_sesame ("file1.bin", "w+b");
FILE *fp2 = open_sesame ("file2.bin", "r+b");
...
fclose (fp1);
fclose (fp2);

  1. The data buffered by printf() is flushed when fflush (stdout) is called, or when the program terminates. If stdout is line-buffered (which is usually the case when the stream is connected to a terminal/console), then the stream will also be flushed when a newline character is encountered. But since the program invokes undefined behaviour, no assumption about the continuation of the execution of the program can be made.

Add a newline in the call to printf(), or call fflush (stdout);.


Footnote:

The standard doesn't define the behaviour, so this rule applies:

Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined. — 7.1.4p1 C18 standard.