如何在vfsc或c++中枚举目录中的所有文件

How can I enumerate all the file in a directory in vfs c or c++?

本文关键字:文件 枚举 vfsc c++      更新时间:2023-10-16

我需要枚举文件夹中的所有文件,然后导航到子文件夹并执行同样的操作(递归?当然)。

理想情况下,算法应该以相同的方式在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()的版本。您可以使用它来获取文件系统对文件的了解。