這各是在LINUX上 列出目的目錄李全部的檔案


#include
#include
#include
#include
#include
#include

void printdir(char *dir, int depth)
{
    DIR*dp;
    struct dirent *entry;
    struct stat statbuf;

    if((dp = opendir(dir)) == NULL)
    {
        printf("cannot open directory: %s\n", dir);
        return;        
    }

    chdir(dir);
    while((entry = readdir(dp)) != NULL)
    {
        lstat(entry->d_name, &statbuf);
        if(S_ISDIR(statbuf.st_mode))
        {
            if(strcmp(".", entry->d_name) == 0 ||
               strcmp("..", entry->d_name) == 0)
                continue;
            printf("%*s%s/\n", depth, "", entry->d_name);
            printdir(entry->d_name, depth+4);
        }
        else
            printf("%*s%s\n", depth, "", entry->d_name);
    }
    chdir("..");
    closedir(dp);
}
int main(int arc ,char *arg[] )
{
    printf("Directory scan of %s \n",arg[1]);
    printdir(arg[1], 0);
    printf("done.\n");

    exit(0);
}





EXAMPLE: scandir is ap

classic> ./scandir ./
Directory scan of ./
f
dirscan.c~
dirscan.c
done.
classic>

arrow
arrow
    全站熱搜

    立你斯 發表在 痞客邦 留言(0) 人氣()