can't open file: to many files open 
Author Message
 can't open file: to many files open

in my program i open and close a lot of files. after about 10000 times
i get an error (perror) "to many files open" and then i can't open files.

i'm sure that i forgot an fclose.

but is there a way to find this bug with a de{*filter*}. i don't know witch files
i forgot to close. my programm is very big and the error cames after
a couple of hours. witout any spezial tools it takes a few days to find
this error or fix it.

i made a breakpoint on every fopen and fclose.
the number of fopen and fclose is the same

but:

#include <stdio.h>
main()
{
   if(!fclose(0))
       perror("can't close file");

Quote:
}

this stupid fclose runs without an error

heeeelllpppp!

bernhard

___________________________________________________________________
Alt F4



Sat, 27 Dec 1997 03:00:00 GMT  
 can't open file: to many files open
What I don't understand is why so many people who post to
c.l.c with questions give invalid return addresses. It's
like they don't want answers.

: but is there a way to find this bug with a de{*filter*}. i don't know witch files
: i forgot to close.

What I'd do in your case is put a printf() in front of every
fclose() and after every fopen(), and print the pointers you're
sending to each. My program would say:

        open returned FFFFFFFF
        open returned AAAAAAAA
        close called with AAAAAAAA
        close called with FFFFFFFF

Careful examination of these pointer values will probably enlighten
you as to what is going on.  NULLs sent to fclose() will be ignored,
which is perhaps disguising the error. If every close line has 00000000,
you're in trouble.

Another thing to try is a counter - every time fopen() is called,
it gets incremented, and every time fclose is called, it gets
subtracted. This way, if the counter value is some reasonable
number like 5 when you run out of file handles, you know
that the closing logic is wrong. If the counter is wrong, like
-3 or some high value, your whole program's logic might be wrong.

Plus, it's possible you actually are running out of file handles.
DOS limits you to the FILES parm, and HP's soft limit is 60 files.
If you have a legitimate program logic need for > 60 files, you
could be exhausting your file handles.

Nothing debugs better than printf. I have a macro that allows me
to put millions of printfs in my code, and make them all disappear
with a compiler option. My trace mode is constructed to tell me
any time something's value changes. I can usually find a bug
within 5-10 lines by using it.

Scott



Sat, 27 Dec 1997 03:00:00 GMT  
 can't open file: to many files open

Quote:
> i'm sure that i forgot an fclose.

> but is there a way to find this bug with a de{*filter*}. i don't know witch files

    One approach (in addition to the tried and true method of adding
    printf()s) may be to dump the information in your program's FILE
    table when the error occurs. This is quite compiler specific, of
    course, but may be helpful. The stdio.h file would be a good
    place to start looking for the definition of FILE. The definitions
    for stdin and stdout may be good places to start looking for the
    name of your program's FILE table.

    Some systems also allow you to list open file handles. Pausing on
    the error and then listing these handles on such a system (for
    example, using Xoper on an Amiga) may be quite a fast way to find
    out which fopen() is causing the problem.

Quote:
> bernhard

--

***     "If pigs had wings, bacon'd be more expensive" - Jonas Glim     ***


Mon, 29 Dec 1997 03:00:00 GMT  
 can't open file: to many files open

Quote:

>                      CAN'T OPEN FILE: TO MANY FILES OPEN

>    11 Jul 1995 16:17:47 GMT
>    Dept. of Math. and Comp.Sci.; Leiden Univ.; Leiden; the Netherlands
>   Post a followup article to newsgroup(s)
>   Send email reply to author: Bernard Zwischenbrugger
>in my program i open and close a lot of files. after about 10000 times
>i get an error (perror) "to many files open" and then i can't open files.

>i'm sure that i forgot an fclose.

>but is there a way to find this bug with a de{*filter*}. i don't know witch files
>i forgot to close. my programm is very big and the error cames after
>a couple of hours. witout any spezial tools it takes a few days to find
>this error or fix it.

>i made a breakpoint on every fopen and fclose.
>the number of fopen and fclose is the same

Did you really sit there for hours and observe the 10000 opens
and closes?  Are you sure you didn't miss one?

Quote:
>but:

>#include <stdio.h>
>main()
>{
>   if(!fclose(0))
>       perror("can't close file");
>}

>this stupid fclose runs without an error

fclose evaluates to 0 if no error occurred.
fclose expects a parameter of typ FILE *, and '0' is not of
that type.
What does the above example have to do with your prevous
problem?

Quote:
>heeeelllpppp!

Try this.

Initialize your FILE * to 0.  Insure your FILE * == 0 before
doing an fopen.  Set your FILE * to 0 after every successful
fclose.
--
Harold Guller



Thu, 01 Jan 1998 03:00:00 GMT  
 can't open file: to many files open

Quote:

>                      CAN'T OPEN FILE: TO MANY FILES OPEN

>    11 Jul 1995 16:17:47 GMT
>    Dept. of Math. and Comp.Sci.; Leiden Univ.; Leiden; the Netherlands
>   Post a followup article to newsgroup(s)
>   Send email reply to author: Bernard Zwischenbrugger
>in my program i open and close a lot of files. after about 10000 times
>i get an error (perror) "to many files open" and then i can't open files.

>i'm sure that i forgot an fclose.

>but is there a way to find this bug with a de{*filter*}. i don't know witch files
>i forgot to close. my programm is very big and the error cames after
>a couple of hours. witout any spezial tools it takes a few days to find
>this error or fix it.

>i made a breakpoint on every fopen and fclose.
>the number of fopen and fclose is the same

Try something like this.

In a common header file used AFTER fopen is declared insert:

#define fopen   myfopen
#define fclose  myfclose

Then add the following (untested and uncompiled) code:

#undef fopen
#undef myfclose

char openfiles[20][100];

FILE *
myfopen(char *filename, *type)
{
        FILE *fp;

        fp = fopen(filename, type);
        if (fp) {
                strcpy(openfiles[fileno(fp)], filename);
        }
        return fp;

Quote:
}

fclose(FILE *stream)
{
        int rval;
        int fno = fileno(stream);
        if ((rval = fclose(stream)) == 0) {
                openfiles[fno][0] = 0;
        }
        return rval;

Quote:
}

void
printfiles(void)
{
        int i;
        for (i=0; i<20;++i) {
                fprintf(stderr, "file %d is %s\n", i. openfiles[i]);
        }

Quote:
}

Now Just call printfiles after fopen fails to see what files are open.  Better
yet, change myfopen to call printfiles when fopen fails.  Note that this
isn't really portable code, but it gives you the right idea.

Dave
--

AT&T Bell Laboratories    (908)946-1107
943 Holmdel Road
Holmdel, NJ 07733



Sun, 04 Jan 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Beginner [Q] Using Canned File Open/Save dialog

2. Difference between MRU File Open and Regular File Open

3. fatal error C1083: Cannot open include file: 'excpt.h': No such file or directory

4. Newbie - Compile error (Cannot open include file: 'xmldom.idl': No such file or directory)

5. BSCMAKE: error BK1506 : cannot open file '.\Debug\StdAfx.sbr': No such file or directory

6. Can't open database file unless filed count matches

7. How to read file while it's opened as a file number in VB

8. Problem opening a file for read while it is open for write

9. opening a file using open method

10. Problem with opening binary files using open()

11. CDaoDatabase::Open() will not open mdb file

12. How to active or open a particular file as soon as the projectwork space is open

 

 
Powered by phpBB® Forum Software