开发者

How can I enumerate all the file in a directory in POSIX/c c++?

开发者 https://www.devze.com 2023-02-15 16:31 出处:网络
I need to enumarate all the file in a dir and then navigate to the subdir and do the same. Ideally the algorithm should work开发者_C百科 in the same way on linux macos [windows(obsolete) no more].

I need to enumarate all the file in a dir and then navigate to the subdir and do the same.

Ideally the algorithm should work开发者_C百科 in the same way on linux macos [windows(obsolete) no more].

UPDATE: I'm now aware of VFS but I'm puzzled to use VFS for enumerate dir. Any suggestion ? Should I open a dir as file ?


POSIX.1-2001 specifies opendir, readdir, and closedir, seekdir, rewinddir, and telldir. Your platform likely has man pages describing how to use them.

These are reportedly not supported directly by MS libraries, instead apparently preferring to use FindFirst and FindNext over there, but there's supposedly several emulation libraries that provide the above; you'll have to sort that part on your own, as I'm not very familiar with Win32.


If you're using GCC, you can try the file system interface. Check out here: GNU file system interface referemce


You can use Boost Filesystem, which is portable to Linux, Windows, and MacOS. The recursive_directory_iterator will allow you, as the name implies, to iterate recursively through a directory.

#include "boost/filesystem.hpp"
#include <iostream>

int main()
{
    namespace fs = boost::filesystem;
    fs::recursive_directory_iterator end;
    for ( fs::recursive_directory_iterator dir("./"); dir != end; ++dir )
    {
       std::cout << *dir << std::endl;
    }
}


You should use getdents() or readdir() on Linux

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号