![](/img/trans.png)
[英]How to be notified of file/directory change in C/C++, ideally using POSIX
[英]How can I enumerate all the file in a directory in POSIX/c c++?
我需要在dir中創建所有文件,然后導航到子目錄並執行相同操作。
理想情況下,算法應該在linux macos上以相同的方式工作[windows(廢棄)不再]。
更新:我現在知道VFS,但我很困惑使用VFS枚舉dir。 有什么建議嗎? 我應該打開dir作為文件嗎?
POSIX.1-2001指定了opendir
, readdir
和closedir
, seekdir
, rewinddir
和telldir
。 您的平台可能包含描述如何使用它們的手冊頁。
據報道,MS庫並不直接支持這些,而是顯然更喜歡在那里使用FindFirst
和FindNext
,但據推測,有幾個仿真庫可以提供上述功能。 你必須自己對那部分進行排序,因為我對Win32不是很熟悉。
如果您正在使用GCC,則可以嘗試文件系統界面。 在這里查看: GNU文件系統接口參考
您可以使用Boost Filesystem ,它可移植到Linux,Windows和MacOS。 正如其名稱所示, recursive_directory_iterator將允許您以遞歸方式遍歷目錄。
#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;
}
}
您應該在Linux上使用getdents()或readdir()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.