#include <dirent.h>
#include <iostream>
DIR *dp; //创建一个指向root路径下每个文件的指针
struct dirent *dirp;
string root="./path/"
if((dp = opendir(root.c_str()))==NULL)
cout << "can't open"<< root << endl;
while((dirp = readdir(dp)) != NULL){
cout<< dirp->d_name << endl; // 输出名字
}
但是要注意的是,输出的前两个路径名是无效的,"." 和 ".." 。
本文介绍了一个使用C++读取Linux系统指定路径下所有文件及文件夹的方法。通过使用dirent.h库中的函数,可以遍历目录并输出每个条目的名称。
1976

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



