#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
if (strcmp(ep->d_name,".") && strcmp(ep->d_name, ".."))
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
This program was tested!
From
Simple Program to List a Directory
本文介绍了一个简单的C语言程序,该程序使用POSIX API来遍历指定目录下的所有文件,并排除当前目录(.)和父目录(..)。此程序适用于学习基本的文件系统操作和目录遍历。

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



