Too many open files: File IO in C on Windows
I have the following code which is just printing out some text and a variable to the file. I am getting the error "Too many open files". I'm using C on Windows in VS2010.
int i, count = 0;
FILE *f;
int _x, _y, _z, _x2, 开发者_StackOverflow_y2, _z2;
for (i = 0; i < HEIGHT * WIDTH*3; i+= 3)
{
if (buffer1[i/3] < MAGIC_VALUE)
{
count++;
}
if (buffer2[i/3] < MAGIC_VALUE)
{
count++;
}
}
printf("Count = %d\n", count); // prints correctly...
f = fopen("file.abc", "w"); // f == NULL. perror gives "Too many open files"
fprintf(f, "lots\n of\n text\n");
fprintf(f, "count: %d\ntext \ntext y\ntext text text", count);
fprintf(f, "\nend");
fclose(f);
I have nothing open besides visual studio when this is running.
If you're getting too many open files, this has to do -- I believe from long ago memory -- this has to do with an environment variable that controls open handles or something similar. I'm searching for your answer and will post it if I find it. This was a good question. I agree with other comments that continually calling fopen -- if that is what is happening -- would cause this problem no matter how large the environment variable setting.
Edit:
My memory could be going back to 16-bit Windows. Mea Culpa.
精彩评论