180*_*ION 96
在标准C++中,从技术上讲,没有办法做到这一点,因为标准C++没有目录概念.如果你想稍微扩展你的网络,你可能想看看使用Boost.FileSystem.这已被接受包含在TR2中,因此这为您提供了尽可能接近标准的最佳实施机会.
一个例子,直接来自网站:
bool find_file( const path & dir_path, // in this directory,
const std::string & file_name, // search for this name,
path & path_found ) // placing path here if found
{
if ( !exists( dir_path ) ) return false;
directory_iterator end_itr; // default construction yields past-the-end
for ( directory_iterator itr( dir_path );
itr != end_itr;
++itr )
{
if ( is_directory(itr->status()) )
{
if ( find_file( itr->path(), file_name, path_found ) ) return true;
}
else if ( itr->leaf() == file_name ) // see below
{
path_found = itr->path();
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Adi*_*vit 45
在带有"Filesystem TS"的C++ <filesystem>11/14中,标题和范围 - for你可以简单地这样做:
#include <filesystem>
using recursive_directory_iterator = std::filesystem::recursive_directory_iterator;
...
for (const auto& dirEntry : recursive_directory_iterator(myPath))
std::cout << dirEntry << std::endl;
Run Code Online (Sandbox Code Playgroud)
从C++ 17开始,它std::filesystem是标准库的一部分,可以在<filesystem>标题中找到(不再是"实验性的").
Jor*_*ira 42
如果使用Win32 API,则可以使用FindFirstFile和FindNextFile函数.
http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx
对于目录的递归遍历,您必须检查每个WIN32_FIND_DATA.dwFileAttributes以检查是否设置了FILE_ATTRIBUTE_DIRECTORY位.如果该位已设置,则可以递归调用该目录的函数.或者,您可以使用堆栈来提供递归调用的相同效果,但避免了很长路径树的堆栈溢出.
#include <windows.h>
#include <string>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
bool ListFiles(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"\\" + ffd.cFileName);
}
else {
files.push_back(path + L"\\" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
int main(int argc, char* argv[])
{
vector<wstring> files;
if (ListFiles(L"F:\\cvsrepos", L"*", files)) {
for (vector<wstring>::iterator it = files.begin();
it != files.end();
++it) {
wcout << it->c_str() << endl;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*u G 31
使用新的基于C++ 11范围for和Boost可以使它更简单:
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
struct recursive_directory_range
{
typedef recursive_directory_iterator iterator;
recursive_directory_range(path p) : p_(p) {}
iterator begin() { return recursive_directory_iterator(p_); }
iterator end() { return recursive_directory_iterator(); }
path p_;
};
for (auto it : recursive_directory_range(dir_path))
{
std::cout << it << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*lex 23
一个快速的解决方案是使用C的Dirent.h库.
来自维基百科的工作代码片段:
#include <stdio.h>
#include <dirent.h>
int listdir(const char *path) {
struct dirent *entry;
DIR *dp;
dp = opendir(path);
if (dp == NULL) {
perror("opendir: Path does not exist or could not be read.");
return -1;
}
while ((entry = readdir(dp)))
puts(entry->d_name);
closedir(dp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
mrv*_*nzo 10
除了上面提到的boost :: filesystem之外,您可能还想检查wxWidgets :: wxDir和Qt :: QDir.
wxWidgets和Qt都是开源的跨平台C++框架.
wxDir提供了一种使用Traverse()简单GetAllFiles()函数递归遍历文件的灵活方法.您也可以使用GetFirst()和GetNext()函数实现遍历(我假设Traverse()和GetAllFiles()是最终使用GetFirst()和GetNext()函数的包装器).
QDir提供对目录结构及其内容的访问.有几种方法可以使用QDir遍历目录.您可以使用QDirIterator :: Subdirectories标志实例化的QDirIterator迭代目录内容(包括子目录).另一种方法是使用QDir的GetEntryList()函数并实现递归遍历.
下面是示例代码(取自这里展示了如何遍历所有子目录#例8-5).
#include <qapplication.h>
#include <qdir.h>
#include <iostream>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QDir currentDir = QDir::current();
currentDir.setFilter( QDir::Dirs );
QStringList entries = currentDir.entryList();
for( QStringList::ConstIterator entry=entries.begin(); entry!=entries.end(); ++entry)
{
std::cout << *entry << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用std::filesystem::recursive_directory_iterator. 但请注意,这包括符号(软)链接。如果你想避免它们,你可以使用is_symlink. 用法示例:
size_t directory_size(const std::filesystem::path& directory)
{
size_t size{ 0 };
for (const auto& entry : std::filesystem::recursive_directory_iterator(directory))
{
if (entry.is_regular_file() && !entry.is_symlink())
{
size += entry.file_size();
}
}
return size;
}
Run Code Online (Sandbox Code Playgroud)
Boost :: filesystem提供了recursive_directory_iterator,这对于这个任务非常方便:
#include "boost/filesystem.hpp"
#include <iostream>
using namespace boost::filesystem;
recursive_directory_iterator end;
for (recursive_directory_iterator it("./"); it != end; ++it) {
std::cout << *it << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
您可能最适合使用 boost 或 c++14 的实验性文件系统内容。如果您正在解析内部目录(即用于您的程序在程序关闭后存储数据),则创建一个索引文件,其中包含文件内容的索引。顺便说一句,您将来可能需要使用 boost,所以如果您没有安装它,请安装它!其次,您可以使用条件编译,例如:
#ifdef WINDOWS //define WINDOWS in your code to compile for windows
#endif
Run Code Online (Sandbox Code Playgroud)
每个案例的代码取自/sf/answers/4713551/
#ifdef POSIX //unix, linux, etc.
#include <stdio.h>
#include <dirent.h>
int listdir(const char *path) {
struct dirent *entry;
DIR *dp;
dp = opendir(path);
if (dp == NULL) {
perror("opendir: Path does not exist or could not be read.");
return -1;
}
while ((entry = readdir(dp)))
puts(entry->d_name);
closedir(dp);
return 0;
}
#endif
#ifdef WINDOWS
#include <windows.h>
#include <string>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;
bool ListFiles(wstring path, wstring mask, vector<wstring>& files) {
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
wstring spec;
stack<wstring> directories;
directories.push(path);
files.clear();
while (!directories.empty()) {
path = directories.top();
spec = path + L"\\" + mask;
directories.pop();
hFind = FindFirstFile(spec.c_str(), &ffd);
if (hFind == INVALID_HANDLE_VALUE) {
return false;
}
do {
if (wcscmp(ffd.cFileName, L".") != 0 &&
wcscmp(ffd.cFileName, L"..") != 0) {
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
directories.push(path + L"\\" + ffd.cFileName);
}
else {
files.push_back(path + L"\\" + ffd.cFileName);
}
}
} while (FindNextFile(hFind, &ffd) != 0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hFind);
return false;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
}
return true;
}
#endif
//so on and so forth.
Run Code Online (Sandbox Code Playgroud)
您可以使用ftw(3)或nftw(3)在POSIX系统上以C或C ++遍历文件系统层次结构。
我们是2019年我们有文件系统的标准库C++。它Filesystem library提供了对文件系统及其组件(例如路径、常规文件和目录)执行操作的工具。
如果您正在考虑可移植性问题,则此链接上有一个重要说明。它说:
如果实现无法访问分层文件系统,或者它不提供必要的功能,则文件系统库设施可能不可用。如果底层文件系统不支持某些功能,则它们可能不可用(例如,FAT 文件系统缺少符号链接并禁止多个硬链接)。在这些情况下,必须报告错误。
文件系统库最初开发为boost.filesystem,作为技术规范 ISO/IEC TS 18822:2015 发布,最终从 C++17 合并到 ISO C++。boost 实现目前在比 C++17 库更多的编译器和平台上可用。
@adi-shavit 在它是 std::experimental 的一部分时已经回答了这个问题,他在 2017 年更新了这个答案。我想提供有关该库的更多详细信息并展示更详细的示例。
std::filesystem::recursive_directory_iterator是一个LegacyInputIterator迭代目录的 directory_entry 元素,并递归地遍历所有子目录的条目。迭代顺序未指定,除了每个目录条目仅访问一次。
如果您不想递归迭代子目录的条目,则应使用directory_iterator。
两个迭代器都返回一个directory_entry对象。directory_entry具有像各种有用的成员函数is_regular_file,is_directory,is_socket,is_symlink等。path()成员函数返回的目的的std ::文件系统::路径,它可以被用来获得file extension,filename,root name。
考虑下面的例子。我一直在使用Ubuntu并在终端上编译它使用
g++ example.cpp --std=c++17 -lstdc++fs -Wall
#include <iostream>
#include <string>
#include <filesystem>
void listFiles(std::string path)
{
for (auto& dirEntry: std::filesystem::recursive_directory_iterator(path)) {
if (!dirEntry.is_regular_file()) {
std::cout << "Directory: " << dirEntry.path() << std::endl;
continue;
}
std::filesystem::path file = dirEntry.path();
std::cout << "Filename: " << file.filename() << " extension: " << file.extension() << std::endl;
}
}
int main()
{
listFiles("./");
return 0;
}
Run Code Online (Sandbox Code Playgroud)