总结:

linux下的两个关键函数

1.     opendir()     打开一个目录

2.     readdir()     从一个目录中读取一个新的文件

 

windows下 读取指定目录的所有文件名字见 博客:


 

实验结果如下图(查找当前目录所有的文件,上面已经用ll输出了整个目录结构,下面是运行打印出来的目录下所有文件名)

c++ linux下读取指定目录的所有文件名字_文件名

源代码

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main()
{
DIR* d = opendir(".");
if (d == NULL)
{
printf("d == NULL");
}

struct dirent* entry;
while ( (entry=readdir(d)) != NULL)
{
puts(entry->d_name);
}

closedir(d);

return 0;
}