C编程在目录中打开目录 #包括 #包括 #包括 #包括 #包括 int程序(char*文件){ DIR*d; 结构方向*dir; d=opendir(文件); 如果(d){ 而((dir=readdir(d))!=NULL){ if(dir->d_type==DT_dir){ if(opendir(dir->d_name)!=NULL){ printf(“打开成功!”); } printf(“是dir!”); } printf(“%s\n”,dir->d_name); } closedir(d); } 返回(0); }
假设我访问c程序中的一个目录,我发现它的一个条目也是一个目录?如何打开此目录项?我认为我的代码做得不正确此代码递归调用函数以列出文件和目录C编程在目录中打开目录 #包括 #包括 #包括 #包括 #包括 int程序(char*文件){ DIR*d; 结构方向*dir; d=opendir(文件); 如果(d){ 而((dir=readdir(d))!=NULL){ if(dir->d_type==DT_dir){ if(opendir(dir->d_name)!=NULL){ printf(“打开成功!”); } printf(“是dir!”); } printf(“%s\n”,dir->d_name); } closedir(d); } 返回(0); },c,C,假设我访问c程序中的一个目录,我发现它的一个条目也是一个目录?如何打开此目录项?我认为我的代码做得不正确此代码递归调用函数以列出文件和目录 #include <dirent.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> int program(char* file) { DIR *d; struc
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int program(char* file) {
DIR *d;
struct dirent *dir;
d = opendir(file);
if (d) {
while ((dir = readdir(d)) != NULL) {
if(dir->d_type == DT_DIR) {
if(opendir(dir->d_name)!=NULL) {
printf("Open successful! ");
}
printf("Is a dir! ");
}
printf("%s\n", dir->d_name);
}
closedir(d);
}
return(0);
}
#包括
#包括
#包括
#包括
#包括
内部主(空){
listdir(“.”,0);//调用当前路径的函数
}
/*函数列出文件和目录
*@parameter Name:Name是路径
*@parameter indent:用于打印文件和目录的缩进
在等级结构中
*/
void listdir(常量字符*名称,整数缩进)
{
DIR*DIR;
结构方向*条目;
/*基本情况下,如果当前文件不是目录,则返回*/
如果(!(dir=opendir(name)))
返回;
/*循环当前目录中的所有文件*/
while((entry=readdir(dir))!=NULL){
如果(条目->数据类型==DT\U目录){
字符路径[1024];
/*如果路径为。(当前路径)或..(当前路径的父路径)
目录),然后继续(不要调用lisdir()),
我们不想陷入无限的境地!
*/
如果(strcmp(条目->数据单元名称,“.”)=0 | | strcmp(条目->数据单元名称,“…”)==0)
继续;
snprintf(路径,sizeof(路径),%s/%s”,名称,条目->d_名称);
printf(“%*s[%s]\n”,缩进,”,条目->数据单元名称);
/*为当前路径再次调用该函数
在缩进中添加2,即在缩进中再留下2个空格
层次结构
*/
listdir(路径,缩进+2);
}否则{
printf(“%*s-%s\n”,缩进,”,条目->数据单元名称);
}
}
closedir(dir);
}
名称是“递归”。顺便提醒一下:对于便携式程序,您不能依赖于d_type
;对于某些文件系统,它可能包含dtu UNKNOWN
。在这种情况下,您需要调用stat()
,以确定它是否是一个目录(另请参见man 3 readdir
)。@xing我是否必须将名称键入路径并打开该路径?另一个旁白:声明const char*file
(而不是非const)的良好做法除非您打算更改它。您还应该注意并检查POSIX函数的使用。
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
int main(void) {
listdir(".", 0); //Call the function for the current path
}
/* Function to list files and directories
* @parameter Name : Name is the path
* @parameter indent: indentation for printing the files and directories
in the hierarchical structure
*/
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
/* Base Case, return if the current file is not directory */
if (!(dir = opendir(name)))
return;
/*Loop for all the files in the current directory */
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
/* If the path is . (current path) or .. (parent of the current
directory) then continue (do not call lisdir()),
we don't want to stuck in infinite situation!
*/
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
/* Call the function again for the current path
Add 2 to indentation, that is to leave 2 more spaces in the
hierarchical structure
*/
listdir(path, indent + 2);
} else {
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}