您的位置:首页 > 运维架构 > Linux

linux下用c实现ls命令

2017-01-04 11:08 281 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: