如何从我的C或c++代码中确定目录中的文件列表?
我不允许在程序中执行ls命令并解析结果。
如何从我的C或c++代码中确定目录中的文件列表?
我不允许在程序中执行ls命令并解析结果。
当前回答
#include<iostream>
#include <dirent.h>
using namespace std;
char ROOT[]={'.'};
void listfiles(char* path){
DIR * dirp = opendir(path);
dirent * dp;
while ( (dp = readdir(dirp)) !=NULL ) {
cout << dp->d_name << " size " << dp->d_reclen<<std::endl;
}
(void)closedir(dirp);
}
int main(int argc, char **argv)
{
char* path;
if (argc>1) path=argv[1]; else path=ROOT;
cout<<"list files in ["<<path<<"]"<<std::endl;
listfiles(path);
return 0;
}
其他回答
这是我想分享的一些东西,谢谢你们的阅读材料。稍微摆弄一下这个函数来理解它。你可能会喜欢的。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;
}
#include <string>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string path = "/path/to/directory";
for (const auto & entry : fs::directory_iterator(path))
std::cout << entry.path() << std::endl;
}
彼得·帕克的解决方案,但没有用于:
#include <algorithm>
#include <filesystem>
#include <ranges>
#include <vector>
using namespace std;
int main() {
vector<filesystem::path> filePaths;
ranges::transform(filesystem::directory_iterator("."),
back_inserter(filePaths), [](const auto& dirFile){return dirFile.path();} );
}
对于C的唯一解决方案,请检查这个。它只需要一个额外的头文件:
https://github.com/cxong/tinydir
tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
printf("%s", file.name);
if (file.is_dir)
{
printf("/");
}
printf("\n");
tinydir_next(&dir);
}
tinydir_close(&dir);
与其他选项相比,以下是一些优势:
它是可移植的-包装POSIX的dirent和Windows的FindFirstFile 它在可用的地方使用readdir_r,这意味着它(通常)是线程安全的 通过相同的UNICODE宏支持Windows UTF-16 它是C90,所以即使是非常古老的编译器也可以使用它
简单地在Linux中使用ASCI C风格的代码
#include <bits/stdc++.h>
#include <dirent.h>
using namespace std;
int main(){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir("./");
if (dpdf != NULL){
while (epdf = readdir(dpdf)){
cout << epdf->d_name << std::endl;
}
}
closedir(dpdf);
return 0;
}
希望这能有所帮助!