c++ - boost 目录迭代器抛出 ERROR_INVALID_DRIVE

标签 c++ boost filesystems

我观察到 boost (v. 1.64) 目录迭代器构造函数 (directory_iterator(const path& p)) 在 Windows 系统上抛出 ERROR_INVALID_DRIVE 异常。我想问题发生在 NTFS 上,但不能 100% 确定。

  1. ERROR_INVALID_DRIVE 的一般原因是什么?
  2. 什么可能将其放入 directory_iterator(const path& p) 中?据我了解,它只是列出了一个目录,因此无法在此处找到获取无效驱动器的方法

更新:

相关代码如下:

namespace bfs = boost::filesystem;

bfs::path cache_path("C:/Users/Администратор/AppData/Local/some_unique_path/");

long long dir_size = 0;

boost::system::error_code ec;

int err_no = 0;

for (bfs::directory_iterator it(cache_path), eit; it != eit; it.increment(ec)) {
    if (ec) {
        std::cout << "Error " << 0 << ":" << ec << " while clearing the cache\n";
        return;
    }

    dir_size += bfs::file_size(it->path(), ec);

    if (ec) {
        std::cout << "Error " << 1 << ":" << ec << " while clearing the cache\n";
        return;
    }
}

dir_size >>= 20; // want current cache size in Mb

if (dir_size > new_size) {
    std::cout << "Clearing the kernel cache..." << std::endl;
    // each file is attempted to be removed
    for (bfs::directory_iterator it(cache_path), eit; it != eit; ++it) {
        if (ec) {
            std::cout << "Error " << 2 << ":" << ec << " while clearing the cache\n";
            return;
        }
        bfs::remove(it->path(), ec); // this ec is skipped: don't care if it was impossible to delete file
    }
}

最佳答案

很高兴看到您的一段代码。通常这意味着该目录不存在。请注意,在 Windows 中,目录路径具有以下格式,例如,"C:\dir1\dir2"。由于您没有提供任何源代码示例,因此很难为您提供帮助。也许您只是提供了错误格式的路径。

关于c++ - boost 目录迭代器抛出 ERROR_INVALID_DRIVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57005825/

相关文章:

c++ - 格式化输出到 C++ ostringstream 对象

c++ - Eclipse CDT 未正确解析 <variant> 包含文件

c++ - OpenCV 边界模式与模糊滤镜的问题

c++ - boost::iostreams::mapped_file_sink 抛出未知异常

c++ - int main() argv 参数问题

c++ - 使用 C++11 拆分字符串

c++ - Boost字符串算法错误

windows - 文件系统过滤器驱动程序 - 拒绝文件创建

database - 存储上传的图像、SQL 数据库或磁盘文件系统的最佳位置是什么?

c++ - MinGW 上 C++17 <文件系统> 的编译错误