C++,Boost,如何获取不再存在的路径名?

标签 c++ boost path filesystems

我正在使用 boost::filesystem遍历给定根目录(比如 C:\\)路径下的所有目录和文件。

我用过recursive_directory_iterator初始化 vector<boost::filesystem::path>包含根目录下所有路径的路径。

然而,一些路径已从目录中删除,我正在打印一条消息说“路径不再存在,路径是......”。但是我不确定如何获取不再存在的路径名(因为我无法访问 vector 来获取路径名)。

按要求添加代码*

for (size_t i = 0; i < this->paths.size(); i++) {
    if (boost::filesystem::exists(this->paths[i])) {
        /* get a path from the list of path, ps */
        boost::filesystem::path p = this->paths[i];

        /* convert path to string */
        string path = p.string();

        /* check file size(should not be over 10MB) */
        if (get_file_size(path) > 10 * pow(10, 6)) continue;

        /* setting extension */
        string file_extension = boost::filesystem::extension(path);

        /* if current path stands for a text file */
        if (!file_extension.compare(this->extension)) notify_changes(p);
    } else {
        cout << "A path no longer exist, the path is.." << endl;
        this->paths.erase(this->paths.begin() + i);
    }
}

最佳答案

循环

老派循环方法:

Live On Coliru

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

namespace fs = boost::filesystem;

int main() {
    if (system("bash -c 'mkdir -pv tree/{a,b,c}/subdir/; touch tree/{a,b,c}/subdir/{a,b,c}.txt'"))
        perror("whoops");

    std::vector<fs::path> paths { fs::recursive_directory_iterator { "tree/" }, {} };

    if (system("bash -c 'rm -rfv tree/b/subdir/ tree/*/subdir/b.txt'"))
        perror("whoops");

    for (auto it = paths.begin(); it != paths.end();) {
        auto& p = *it;
        if (!fs::exists(p))
        {
            std::cout << "No longer exists: " << p << "\n";
            it = paths.erase(it);
            continue;
        }

        // DO PROCESSING

        ++it;
    }
}

使用算法

Live On Coliru

#include <boost/filesystem.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>

namespace fs = boost::filesystem;

bool try_to_process(fs::path const& p) // return false if file wasn't found
{
    if (!fs::exists(p))
    {
        std::cout << "No longer exists: " << p << "\n";
        return false;
    }

    // DO PROCESSING

    return true;
}

int main() {
    if (system("bash -c 'mkdir -pv tree/{a,b,c}/subdir/; touch tree/{a,b,c}/subdir/{a,b,c}.txt'"))
        perror("whoops");

    std::vector<fs::path> paths { fs::recursive_directory_iterator { "tree/" }, {} };

    if (system("bash -c 'rm -rfv tree/b/subdir/ tree/*/subdir/b.txt'"))
        perror("whoops");

    paths.erase(boost::stable_partition(paths, try_to_process), paths.end());
}

输出

打印

mkdir: created directory `tree'
mkdir: created directory `tree/a'
mkdir: created directory `tree/a/subdir/'
mkdir: created directory `tree/b'
mkdir: created directory `tree/b/subdir/'
mkdir: created directory `tree/c'
mkdir: created directory `tree/c/subdir/'
removed `tree/b/subdir/c.txt'
removed `tree/b/subdir/b.txt'
removed `tree/b/subdir/a.txt'
removed directory: `tree/b/subdir'
removed `tree/a/subdir/b.txt'
removed `tree/c/subdir/b.txt'
No longer exists: "tree/b/subdir"
No longer exists: "tree/b/subdir/c.txt"
No longer exists: "tree/b/subdir/b.txt"
No longer exists: "tree/b/subdir/a.txt"
No longer exists: "tree/a/subdir/b.txt"
No longer exists: "tree/c/subdir/b.txt"

关于C++,Boost,如何获取不再存在的路径名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40524520/

相关文章:

c++ - 在 OnInitDialog() 中断言失败

使用 Obj 模型的 C++ 碰撞

c++ - 在 Visual C++ 中链接 libboost_system 时出错

path - 在 Ansible 中获取父目录?

c++ - 如何将不同目录的cpp文件编译到一个文件夹中?

c++ - lapacke 与 boost 冲突?

c++ - 我可以在没有结构实例的情况下使用 `hana::keys` 吗?

javascript - 删除名称相同但路径不同的cookie

c++ - boost最短路径查找算法

c++ - 客户端服务器编程缓冲区内容不正确