
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