如何在vfsc或c++中枚举目录中的所有文件
How can I enumerate all the file in a directory in vfs c or c++?
我需要枚举文件夹中的所有文件,然后导航到子文件夹并执行同样的操作(递归?当然)。
理想情况下,算法应该以相同的方式在linux&macos
免责声明:我在POSIX上问了一个类似的问题:我现在知道VFS,但我对使用VFS枚举目录感到困惑。有什么建议吗?我应该打开目录作为文件吗?唯一的方法是使用像qt?
更新:所以没有VFS在目录上工作的方法?"*V*irtual*F*文件*2S**系统提供了一个单一的API来访问各种不同的文件系统",但无法枚举目录。
"readdir"等解决方案将在任何类型的*NIX上发挥作用?在窗户上,没有什么比巨大的MingW lib更好的了吗?或者只在一些胜利上工作的部分暗示,比如:https://github.com/xbmc/xbmc/blob/master/tools/TexturePacker/Win32/dirent.c
BOOST似乎是一个非常酷的解决方案,但它既复杂又学术。在任何情况下都是
上次更新:
我找到了更多的医生,现在一切都清楚多了。这个问题是重复的opendir()和readdir()是在linux上枚举和浏览目录的解决方案。如我的例子所示,在windows上映射它们非常容易(但固有的windowz fs会让一切变得奇怪),ntfw()更有用。
VFS(虚拟文件交换机)是一个内核功能,它通过为文件系统操作创建抽象层来解决这个问题。关闭这里的文档:linux编程接口
thnks!
您想要查看nftw
。这里有一个例子,它只是递归地打印C(Untested)中目录的内容:
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ftw.h>
int
print( const char *path, const struct stat *s, int flag, struct FTW *f )
{
puts( path );
return 0;
}
int
main( int argc, char **argv )
{
while( *++argv ) {
if( nftw( *argv, print, 1024, FTW_DEPTH )) {
perror( *argv );
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
以下是我如何使用Boost.Filesystem:
#include "boost/filesystem.hpp"
#include <iostream>
int main () {
for ( boost::filesystem::recursive_directory_iterator end, dir("./");
dir != end; ++dir ) {
// std::cout << *dir << "n"; // full path
std::cout << dir->path().filename() << "n"; // just last bit
}
}
或者,更简洁地说:
#include "boost/filesystem.hpp"
#include <iostream>
#include <iterator>
#include <algorithm>
int main () {
std::copy(
boost::filesystem::recursive_directory_iterator("./"),
boost::filesystem::recursive_directory_iterator(),
std::ostream_iterator<boost::filesystem::directory_entry>(std::cout, "n"));
}
Unix/Linux/Windows都有readdir()的版本。您可以使用它来获取文件系统对文件的了解。
- 如何在公共头文件中向库的用户公开枚举,同时在内部使用它?
- 将枚举传递到不同文件中的对象中
- 枚举前向声明与头文件
- 在编译时从 c++ 文件创建枚举
- C++-使用来自不同头文件、相同命名空间的枚举
- 如何将枚举类写入文件并在C++中访问它?
- 从另一个C++文件中C++ "Importing"枚举类
- 如何在Qt.js文件中使用C++枚举
- 无法使用其他文件中的枚举:" does not name a type"错误
- 如何将枚举类型数据保存到文件
- C++ 在头文件中使用枚举'___'而没有先前的声明?
- 将枚举传递给另一个文件?C++
- 为了简洁起见,在另一个文件中定义前向声明的嵌套结构/类/枚举
- 当在matlab mex文件中动态链接时,枚举的typeid会发生变化
- 从.cpp文件(C++)中的头文件读取枚举
- c 如何从类类 .h 文件内部识别.cpp中的 UDT 枚举
- 不同文件中同一命名空间中的 2 个不同枚举中的相同标识符
- 如何对文件文档枚举进行多行
- 枚举 Windows 中特定类型的文件
- 枚举可执行文件C++派生类