dirent.h

#include <dirent.h>
是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数、readdir函数.
opendir函数:
DIR *opendir(const char *pathname);返回值:若成功则返回指针,若出错则返回NULL。
struct dirent *readdir(DIR *dp); 返回值:若成功则返回指针,若在目录结尾或出错则返回NULL。
===============================================================================
列出一个目录下所有文件的名字,简要实现unix下ls命令
#include<dirent.h> 
 
intmain(intargc,char*agrv[]) 
{ 
DIR*dp; 
structdirent*dirp; 
 
if(argc!=2) 
printf("usage:lsdirectory_name
"); 
if((dp=opendir(agrv[1]))==NULL) 
printf("cannotopen%s",agrv[1]); 
while((dirp=readdir(dp))!=NULL) 
printf("%s
",dirp->d_name); 
closedir(dp); 
 
return0; 
 } 
原文地址:https://www.cnblogs.com/zendu/p/4980761.html