如何从我的C或c++代码中确定目录中的文件列表?
我不允许在程序中执行ls命令并解析结果。
如何从我的C或c++代码中确定目录中的文件列表?
我不允许在程序中执行ls命令并解析结果。
当前回答
GNU手册FTW
http://www.gnu.org/software/libc/manual/html_node/Simple-Directory-Lister.html#Simple-Directory-Lister
此外,有时直接找到源头是件好事(双关)。通过查看Linux中一些最常见命令的内部结构,您可以学到很多东西。我在github上建立了一个GNU coreutils的简单镜像(供阅读)。
https://github.com/homer6/gnu_coreutils/blob/master/src/ls.c
也许这并不能解决Windows的问题,但是通过使用这些方法,可以实现使用Unix变体的许多情况。
希望这对你有所帮助……
其他回答
系统调用它!
system( "dir /b /s /a-d * > file_names.txt" );
那就读一读文件。
编辑:这个答案应该被认为是一种hack,但如果你没有更优雅的解决方案,它确实有用(尽管是在特定的平台上)。
我试图遵循两个回答中给出的示例,可能值得注意的是,似乎std::filesystem::directory_entry已被更改为不重载<<操作符。而不是std::cout << p << std::endl;我必须使用以下能够编译并让它工作:
#include <iostream>
#include <filesystem>
#include <string>
namespace fs = std::filesystem;
int main() {
std::string path = "/path/to/directory";
for(const auto& p : fs::directory_iterator(path))
std::cout << p.path() << std::endl;
}
试图将p单独传递给std::cout <<会导致丢失重载错误。
为什么不使用glob()?
#include <glob.h>
glob_t glob_result;
glob("/your_directory/*",GLOB_TILDE,NULL,&glob_result);
for(unsigned int i=0; i<glob_result.gl_pathc; ++i){
cout << glob_result.gl_pathv[i] << endl;
}
这是我想分享的一些东西,谢谢你们的阅读材料。稍微摆弄一下这个函数来理解它。你可能会喜欢的。E表示扩展,p表示路径,s表示路径分隔符。
如果传递路径时没有结束分隔符,则将在路径后附加分隔符。对于扩展名,如果输入的是空字符串,则函数将返回名称中没有扩展名的任何文件。如果只输入一个星号,则将返回目录中的所有文件。如果e的长度大于0,但不是一个*,那么如果e在0点位置没有包含一个点,则会在e前面加上一个点。
对于返回值。如果返回一个零长度的映射,则什么也没有找到,但是目录是打开的。如果返回值中索引999是可用的,但映射大小只有1,那么这意味着打开目录路径有问题。
注意,为了提高效率,这个函数可以分成3个更小的函数。在此基础上,您可以创建一个调用方函数,该函数将根据输入检测将要调用的函数。为什么这样更有效率?也就是说,如果你要抓取所有属于文件的东西,执行那个方法这个为抓取所有文件而构建的子函数只会抓取所有属于文件的东西而不需要在每次找到一个文件时评估任何其他不必要的条件。
这也适用于抓取没有扩展名的文件。为此目的构建的特定函数只在找到的对象是一个文件时计算天气,然后计算文件名称中是否有点。
如果只读取文件不多的目录,节省的时间可能不多。但是,如果您正在读取大量的目录,或者如果目录有数十万个文件,这可能是一个巨大的节省。
#include <stdio.h>
#include <sys/stat.h>
#include <iostream>
#include <dirent.h>
#include <map>
std::map<int, std::string> getFile(std::string p, std::string e = "", unsigned char s = '/'){
if ( p.size() > 0 ){
if (p.back() != s) p += s;
}
if ( e.size() > 0 ){
if ( e.at(0) != '.' && !(e.size() == 1 && e.at(0) == '*') ) e = "." + e;
}
DIR *dir;
struct dirent *ent;
struct stat sb;
std::map<int, std::string> r = {{999, "FAILED"}};
std::string temp;
int f = 0;
bool fd;
if ( (dir = opendir(p.c_str())) != NULL ){
r.erase (999);
while ((ent = readdir (dir)) != NULL){
temp = ent->d_name;
fd = temp.find(".") != std::string::npos? true : false;
temp = p + temp;
if (stat(temp.c_str(), &sb) == 0 && S_ISREG(sb.st_mode)){
if ( e.size() == 1 && e.at(0) == '*' ){
r[f] = temp;
f++;
} else {
if (e.size() == 0){
if ( fd == false ){
r[f] = temp;
f++;
}
continue;
}
if (e.size() > temp.size()) continue;
if ( temp.substr(temp.size() - e.size()) == e ){
r[f] = temp;
f++;
}
}
}
}
closedir(dir);
return r;
} else {
return r;
}
}
void printMap(auto &m){
for (const auto &p : m) {
std::cout << "m[" << p.first << "] = " << p.second << std::endl;
}
}
int main(){
std::map<int, std::string> k = getFile("./", "");
printMap(k);
return 0;
}
Shreevardhan的设计也适用于遍历子目录:
#include <string>
#include <iostream>
#include <filesystem>
using namespace std;
namespace fs = filesystem;
int main()
{
string path = "\\path\\to\\directory";
// string path = "/path/to/directory";
for (auto & p : fs::recursive_directory_iterator(path))
cout << p.path() << endl;
}
编译:cl /EHsc /W4 /WX /std:c++17 ListFiles.cpp