以下是用C++实现搜索文件路径的示例代码:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

void search(fs::path dir_path, const std::string& target_name) {
    for (const auto& entry : fs::directory_iterator(dir_path)) {
        if (entry.is_directory()) {
            search(entry.path(), target_name);
        } else {
            if (entry.path().filename() == target_name) {
                std::cout << "Found " << target_name << " at " << entry.path().string() << std::endl;
            }
        }
    }
}

int main() {
    fs::path dir_path = "C:/Users/";
    std::string target_name = "Desktop";
    search(dir_path, target_name);
    return 0;
}

此示例中,使用了C++17中的filesystem库来遍历目录和文件。在search函数中,如果遇到一个目录,就递归调用自己;如果遇到一个文件,就检查文件名是否与目标文件名相同,如果是,则打印出文件路径。在main函数中,指定了要搜索的目录和目标文件名,并调用search函数来执行搜索

用C++实现搜索文件路径

原文地址: https://www.cveoy.top/t/topic/d0Lu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录