打开文件太多:Windows 上 C 中的文件 IO
我有以下代码,它只是打印一些文本和一个变量到文件中。我收到错误“打开的文件太多”。我在VS2010中使用C on Windows。
int i, count = 0;
FILE *f;
int _x, _y, _z, _x2, _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);
当它运行时,除了 Visual Studio 之外,我没有打开任何东西。
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, _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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你打开的文件太多,这与控制打开句柄或类似内容的环境变量有关——我相信从很久以前的记忆来看——这与控制打开句柄的环境变量有关。我正在寻找您的答案,如果找到,我会发布它。这是一个好问题。我同意其他评论,即不断调用 fopen(如果确实发生了这种情况),无论环境变量设置有多大,都会导致此问题。
编辑:
我的记忆可能会回到 16 位 Windows。罪责。
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.