linux下用c实现ls命令

struct dirent中的几个成员:

d_type:4表示为目录,8表示为文件

d_reclen:16表示子目录或文件,24表示非子目录

d_name:目录或文件的名称

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
int main(int argc,char* argv[])
{
     DIR* dir = opendir(".");
     struct dirent* ent=NULL;
     while((ent = readdir(dir)))
     {
         if((ent->d_type == 4||ent->d_type == 8)&&ent->d_name[0]!='.')
         printf("%s  ",ent->d_name);
     }
     closedir(dir);
     puts("");
     return 0;
}

 

posted @ 2017-01-04 11:08  Ritchie丶  阅读(2243)  评论(0编辑  收藏  举报