8

What happens if i open a file using fopen some n number of times without calling fclose on it?

Any buffer overrun issues may arise?

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
chaitanyavarma
  • 315
  • 2
  • 4
  • 8

5 Answers5

16

If you continue to fopen without fclose then eventually your future calls to fopen will start to fail. There are a limited number of file descriptors available to your program.

See this related SO question.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Just for further information, I recollect from the early days of DOS that you could set the number of available files that could be opened as an environment variable - up to a point, of course. I believe the default may have been 25, and for some apps definitely needed to be set higher since some of these were used by the OS itself. – MickeyfAgain_BeforeExitOfSO Jul 28 '10 at 14:23
  • 1
    Yes, exactly the same happened.The fopen failed after some 1000 calls.I closed it, thanks. – chaitanyavarma Jul 29 '10 at 18:27
6

You waste underlying file handles. Please close any files you open, in a timely fashion, as to avoid this leak of resources.

NT_
  • 2,660
  • 23
  • 25
  • 3
    Also, calling `fclose` ensures that any data written to the file is not lost in case the application aborts. – lhf Jul 28 '10 at 13:27
  • 5
    You make it sound like there's a giant global pool of file handles that all computers across the world share. Please recycle your file handles. :) – jdizzle Jul 28 '10 at 13:45
  • 2
    @jdizzle: rationing starts next week, grab all you can now! – bstpierre Jul 28 '10 at 15:14
4

If you continue opening files without closing them then you will run out of file descriptors at some point, either at the application level or the OS level, and all further attempts to open a file will fail.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

Aside from wasting file descriptors for the process as others answered, you would also waste memory since each file stream manages in/out buffers that are allocated internally by libc.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

As others have mentioned, you don't want to leak file descriptors. But it is ok to have a single file open multiple times. The file descriptors are independent and will not interfere with each other (assuming you are just reading and not writing to the file).

#include <stdio.h>

int main()
{
    FILE* f1 = fopen("/tmp/foo.txt", "rb");
    FILE* f2 = fopen("/tmp/foo.txt", "rb");
    FILE* f3 = fopen("/tmp/foo.txt", "rb");
    FILE* f4 = fopen("/tmp/foo.txt", "rb");

    char buf1[32] = { 0, };
    char buf2[32] = { 0, };
    char buf3[32] = { 0, };
    char buf4[32] = { 0, };

    fread(buf1, 1, sizeof(buf1) - 1, f1);
    fread(buf2, 1, sizeof(buf2) - 1, f2);
    fread(buf3, 1, sizeof(buf3) - 1, f3);
    fread(buf4, 1, sizeof(buf4) - 1, f4);

    printf("buf1 = '%s'\n", buf1);
    printf("buf2 = '%s'\n", buf2);
    printf("buf3 = '%s'\n", buf3);
    printf("buf4 = '%s'\n", buf4);

    fclose(f1);
    fclose(f2);
    fclose(f3);
    fclose(f4);

    return 0;
}

Gives output like:

$ ./fopen 
buf1 = '0123456789ABCDEFGHIJ0123456789a'
buf2 = '0123456789ABCDEFGHIJ0123456789a'
buf3 = '0123456789ABCDEFGHIJ0123456789a'
buf4 = '0123456789ABCDEFGHIJ0123456789a'
bstpierre
  • 30,042
  • 15
  • 70
  • 103