在linux下遍历某一目录下内容LINUX下历遍目录的方法一般是这样的
打开目录->读取->关闭目录
相关函数是opendir -> readdir -> closedir,其原型如下:
#include
DIR *opendir(const char *dirname);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
简单列举一例:
#include
structdirent*ent=NULL;
DIR*pDir;if( (pDir=opendir("/home/test"))==NULL)
{
printf("open dir %s failed\n", pszBaseDir);returnfalse;
}while( (ent=readdir(pDir))!=NULL )
{
printf("the ent->d_reclen is%d the ent->d_type is%d the ent->d_name is%s\n", ent->d_reclen, ent->d_type, ent->d_name);
}
closedir(pDir);
其中有一很关键的结构体dirent:
代码
On Linux, the dirent structureisdefine