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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM