Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用S#u ISDIR Don';他似乎不总是工作_C_Directory_Stat_Readdir - Fatal编程技术网

C 使用S#u ISDIR Don';他似乎不总是工作

C 使用S#u ISDIR Don';他似乎不总是工作,c,directory,stat,readdir,C,Directory,Stat,Readdir,我注意到我的函数(在c语言中)的输出有些奇怪。 此函数用于检测目录中的元素是文件还是子目录 //我无法正确检测元素是文件还是目录 int递归搜索(char*Dir){ DIR*目录; //DIR*子目录; 结构方向*条目; struct stat filestat; printf(“我正在读取%s目录\n”,Dir); Directory=opendir(Dir); if(目录==NULL) { perror(“无法读取目录..我要离开\n”); 返回(1);//离开 } /*读取目录项*/ 而

我注意到我的函数(在c语言中)的输出有些奇怪。 此函数用于检测目录中的元素是文件还是子目录

//我无法正确检测元素是文件还是目录
int递归搜索(char*Dir){
DIR*目录;
//DIR*子目录;
结构方向*条目;
struct stat filestat;
printf(“我正在读取%s目录\n”,Dir);
Directory=opendir(Dir);
if(目录==NULL)
{
perror(“无法读取目录..我要离开\n”);
返回(1);//离开
}
/*读取目录项*/
而((entry=readdir(Directory)))
{
stat(条目->数据单元名称和文件状态);
if(S_ISDIR(文件状态st_模式)){
printf(“%4s:%s\n”,“Dir”,条目->数据单元名称);
如果(strstr(entry->d_name,“.”)==NULL&&strstrstr(entry->d_name,“…”)==NULL)//不无限循环
{
//递归
printf(“\n*输入子目录*\n”);
递归搜索(条目->数据单元名称);
printf(“\n*离开子目录*\n”);
}
}
其他的
printf(“%4s:%s\n”,“文件”,条目->文件名);
}
closedir(目录);
返回(0);
}
我主要这样称呼它来测试它
RecursiveSearch(“./目录”)

这是我的输出

我的问题是“开始递归研究…”

正如您所看到的,SubDir不是一个文件。 我看不出我在函数中做错了什么

如果您需要任何额外的澄清,请询问

编辑:

int递归搜索(char*Dir){
DIR*目录;
结构方向*条目;
struct stat filestat;
printf(“我正在读取%s目录\n”,Dir);
Directory=opendir(Dir);
if(目录==NULL)
{
perror(“无法读取目录..我要离开\n”);
返回(1);//离开
}
/*读取目录项*/
而((entry=readdir(Directory)))
{
字符全名[100];
sprintf(全名,“%s/%s”,目录,条目->d_名称);
stat(全名和文件状态);
if(S_ISDIR(文件状态st_模式)){
printf(“%4s:%s\n”,“Dir”,全名);
如果(strcmp(entry->d_name,“.”!=0&&strcmp(entry->d_name,“…”)!=0)//非无限循环
{
//递归
printf(“\n*输入子目录*\n”);
递归搜索(全名);
printf(“\n*离开子目录*\n”);
}
}
其他的
printf(“%4s:%s\n”,“文件”,全名);
}
closedir(目录);
返回(0);
}

entry->d_name
只是名称,没有目录前缀。它是相对于进程的工作目录而不是函数递归中的当前目录进行解释的

调用其他函数(包括递归)时,需要在名称前面加上目录名

此外,您需要使用
strcmp()
将名称与
进行比较。
。使用
strstr()
可防止递归到名称中任何位置都有
的目录中

int RecursiveSearch(char *Dir){
    DIR *Directory;
    struct dirent *entry;
    struct stat filestat;

    printf("I am Reading %s Directory\n", Dir);

    Directory = opendir(Dir);
    if(Directory == NULL)
    {
        perror("Unable to read directory.. i'm leaving\n");
        return(1); // leave
    }

    /* Read directory entries */
    while( (entry=readdir(Directory)) )
    {
        char fullname[MAXPATH];
        sprintf(fullname, "%s/%s", Dir, entry->d_name);
        stat(fullname,&filestat);
        if( S_ISDIR(filestat.st_mode) ){
            printf("%4s: %s\n","Dir",fullname);
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) // to not infinite loop
            {
                // Recursion
                printf("\n*Entering a subDirectory*\n");
                RecursiveSearch(fullname);
                printf("\n*Leaving a subDirectory*\n");
            }
        }
        else
            printf("%4s: %s\n","File",fullname);
    }
    closedir(Directory);

    return(0);
}

entry->d_name
只是名称,没有目录前缀。它是相对于进程的工作目录而不是函数递归中的当前目录进行解释的

调用其他函数(包括递归)时,需要在名称前面加上目录名

此外,您需要使用
strcmp()
将名称与
进行比较。
。使用
strstr()
可防止递归到名称中任何位置都有
的目录中

int RecursiveSearch(char *Dir){
    DIR *Directory;
    struct dirent *entry;
    struct stat filestat;

    printf("I am Reading %s Directory\n", Dir);

    Directory = opendir(Dir);
    if(Directory == NULL)
    {
        perror("Unable to read directory.. i'm leaving\n");
        return(1); // leave
    }

    /* Read directory entries */
    while( (entry=readdir(Directory)) )
    {
        char fullname[MAXPATH];
        sprintf(fullname, "%s/%s", Dir, entry->d_name);
        stat(fullname,&filestat);
        if( S_ISDIR(filestat.st_mode) ){
            printf("%4s: %s\n","Dir",fullname);
            if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 ) // to not infinite loop
            {
                // Recursion
                printf("\n*Entering a subDirectory*\n");
                RecursiveSearch(fullname);
                printf("\n*Leaving a subDirectory*\n");
            }
        }
        else
            printf("%4s: %s\n","File",fullname);
    }
    closedir(Directory);

    return(0);
}

嘿,谢谢,我已经在我的帖子中更新并更正了我的功能。它似乎起作用了。我想你在sprintf中有一个错误,我把它改成了
sprintf(全名,“%s/%s”,Dir,entry->d_name)嘿,谢谢,我已经在我的帖子中更新并更正了我的函数。它似乎起作用了。我想你在sprintf中有一个错误,我把它改成了
sprintf(全名,“%s/%s”,Dir,entry->d_name)