#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
void getdir(char *dirname)
{
DIR *MYDIR;
struct dirent *diritem;
if((MYDIR=opendir(dirname)) == NULL)
perror("open dir error!/n");
while((diritem = readdir(MYDIR)) != NULL)
{
if((diritem->d_type == DT_DIR) && (strcmp(diritem->d_name, ".") != 0) && (strcmp(diritem->d_name, "..") != 0))
{
// printf("%s is a directory!/n",diritem->d_name);
printf("/nchange directory into %s:/n",diritem->d_name);
getdir(diritem->d_name);
}
else
{
printf("%s/n",diritem->d_name);
}
}
closedir(MYDIR);
}
int main(int argc, char* argv[])
{
getdir(argv[1]);
}
本文提供了一个使用C语言实现的目录遍历程序示例。该程序利用了POSIX标准下的目录操作函数,能够递归地列出指定目录及其子目录下的所有文件和子目录名称。适用于希望了解如何在类UNIX系统中进行目录遍历的开发者。
1708

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



