首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

读一个目录流的时候是不是一定要再该目录上

2013-01-08 
读一个目录流的时候是不是一定要再该目录下这是code 这个目录下本来的文件:比如里面的threestack 子目录

读一个目录流的时候是不是一定要再该目录下



这是code 这个目录下本来的文件:读一个目录流的时候是不是一定要再该目录上

比如里面的threestack 子目录 里面是有文件的··但是它只打印了目录本身

[解决办法]
void print_dir(char *dir,int depth)
{
    DIR *dp=opendir(dir);
    struct dirent *dirent;
    struct stat stat_buf;
    char path[255] = {0};

    if(dp==NULL)
    {
        fprintf(stderr,"opendir function is error\n");
        exit(EXIT_FAILURE);
    }


    while((dirent=readdir(dp))!=NULL)
    {

        memset(path, 0, 255);
        if(dir[strlen(dir)-1] == '/')
                snprintf(path, 255, "%s", dir);
        else
                snprintf(path, 255, "%s/", dir);
        snprintf(path+strlen(path), 255, "%s", dirent->d_name);

        lstat(path,&stat_buf);
        if(S_ISDIR(stat_buf.st_mode))
        {
            if(strcmp(".",dirent->d_name)==0
[解决办法]

                strcmp("..",dirent->d_name)==0)
                continue;


            printf("Dir %*s%s\n",depth," ",dirent->d_name);

            printf("path = %s\n", path);
            print_dir(path,depth+4);


        }
        else
            printf("File %*s%s\n",depth," ",dirent->d_name);
    }
    //chdir("..");......
    closedir(dp);
}

热点排行