#include <dirent.h>
#include <sys/stat.h>
#include <stdio.h>
int main()
{
DIR *dp;
struct dirent *dirp;
struct stat statbuf;
if((dp=opendir("./"))==NULL)
{
printf("目录名不正确/n");
return -1;
}
int num=0;
while((dirp=readdir(dp))!=NULL)
{
lstat(dirp->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
continue;
printf("is dir:%s/n",dirp->d_name);
}
else
printf("%s/n",dirp->d_name);
num++;
}
return 0;
closedir(dp);
}
本文提供了一个使用C语言进行目录遍历的示例程序。该程序利用了dirent.h库中的函数来读取指定目录下的所有文件及子目录,并通过sys/stat.h库中的函数判断每个条目是否为目录。此外,程序还排除了当前目录(.)和上级目录(..)。此示例适用于想要了解如何用C语言实现基本文件系统操作的开发者。

4662

被折叠的 条评论
为什么被折叠?



