执行opendir:打开的文件太多 [英] opendir: Too many open files
问题描述
在我写这篇code打印中的所有文件 /家庭/保持
与赦免路径:
I write this code to print all files in /home/keep
with absolution path:
#include <dirent.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
void catDIR(const char *re_path);
int main(int argc, char *argv[])
{
char *top_path = "/home/keep";
catDIR(top_path);
return 0;
}
void catDIR(const char *re_path)
{
DIR *dp;
struct stat file_info;
struct dirent *entry;
if ((dp = opendir(re_path)) == NULL)
{
perror("opendir");
return;
}
while (entry = readdir(dp))
{
if (entry->d_name[0] == '.')
continue;
//get Absolute path
char next_path[PATH_MAX];
strcpy(next_path, re_path);
strcat(next_path, "/");
strcat(next_path, entry->d_name);
lstat(next_path, &file_info);
if (S_ISDIR(file_info.st_mode))
{
catDIR(next_path);
}
else
{
printf("%s/%s\n", re_path, entry->d_name);
}
}
free(dp);
free(entry);
}
当我运行它。
不仅打印一些文件的路径,还打印一些错误信息:
Not only print some file's path, but also print some error message:
执行opendir:打开的文件太多
我读男子3执行opendir
,随后发现,我已经打开太多的文件。
I read man 3 opendir
, then realize, I had opened too many files.
我也想知道,怎么关闭它?以及如何纠正这一计划
I do want to know, how to close it? and how to correct this program
推荐答案
您也许应该使用 closedir
当您完成通过目录中的内容进行迭代。
You should probably use closedir
when you finish iterating through a directory's contents.
此外,您可能要读取目录的上市到一个数组,关闭目录,然后阵列上的递归。这可能帮助遍历非常深的目录结构。
Also, you might want to read the directory's listing into an array, close the directory, and then recurse on the array. This might help with traversing very deep directory structures.
这篇关于执行opendir:打开的文件太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!