C++ 读取yaml文件的列表项并复制

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

C++ 读取yaml文件中的列表项,并利用列表项中的数据进行复制


前言

主要用到的知识点**(注意:yaml-cpp/yaml.h需要自行导入**):
1,filesystem相关的库函数操作(copy、exists等)
2,列表项中的属性的读取

这里的案列流程如下:
1,读取test.yaml文件中的列表项并得到列表项中的属性path
2,根据列表项提供的路径path,将该path路径下的yaml文件复制到目标文件下


一、完整代码和注释

1.引入库

#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <filesystem>

using namespace std;
namespace fs = std::filesystem;

2.主要代码

void copyYAMLFiles(const std::string& sour_path, const std::string& dest_path) {
    // 读取当前的 YAML 文件
    YAML::Node config = YAML::LoadFile(sour_path);

    // 从YAML文件中提取路径列表
    if (!config["devices"] || !config["devices"].IsSequence()) {
        std::cerr << "未在 YAML 文件中找到 'devices' 参数或参数不是列表。" << std::endl;
        return;
    }

    // 遍历列表中的每个路径
    for (const auto& node : config["devices"]) {
        // 这里可以多设置几个必须用到的属性
        if (!node["name"] || !node["path"]) {
            std::cerr << "YAML 列表项中缺少 'name' 或 'path' 参数。" << std::endl;
            continue;
        }

        std::string name = node["name"].as<std::string>();
        fs::path sourcePath = node["path"].as<std::string>();

        // 根据name进行判断,决定是否处理当前路径
        if (name == "dev1") {
            // 检查sourcePath是否存在
            if (!fs::exists(sourcePath)) {
                std::cerr << "源文件路径不存在:" << sourcePath << std::endl;
                continue; // 如果路径不存在,跳过这个路径
            }

            // 获取文件名,并拼接到目标目录路径
            fs::path destinationPath = fs::path(dest_path) / sourcePath.filename();

            try {
                // 复制文件到目标路径
                fs::copy(sourcePath, destinationPath, fs::copy_options::overwrite_existing);
                std::cout << "文件已成功复制到:" << destinationPath << std::endl;
            } catch (const fs::filesystem_error& e) {
                std::cerr << "文件复制失败:" << e.what() << std::endl;
            }
        }
    }
}

int main() {
    // 源 YAML 文件的路径
    std::string sour_path = "test.yaml";

    // 目标目录路径
    std::string test_result = "./test_result";

    // 执行文件复制操作
    copyYAMLFiles(sour_path, test_result);

    return 0;
}


二,代码演示结果

1,test.yaml内容

在这里插入图片描述

2,/test/test1.yaml

在这里插入图片描述

3,代码复制结果(/test_result/dev1.yaml)

在这里插入图片描述

要在C++读写YAML文件,您可以使用第三方库来处理YAML格式。以下是使用`yaml-cpp`库读写YAML文件的简单示例: 首先,您需要安装`yaml-cpp`库。您可以从https://github.com/jbeder/yaml-cpp下载源代码,并根据其提供的说明进行安装。 接下来,您可以使用以下代码示例来读取YAML文件: ```cpp #include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> int main() { // 打开YAML文件 std::ifstream file("example.yaml"); // 解析YAML文件 YAML::Node config = YAML::Load(file); // 读取YAML的数据 std::string name = config["name"].as<std::string>(); int age = config["age"].as<int>(); // 打印读取的数据 std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; return 0; } ``` 在上面的示例,我们打开了名为`example.yaml`的YAML文件,并使用`YAML::Load`函数解析了文件内容。然后,我们可以使用`[]`操作符来访问读取的数据,并使用`as`函数将其转换为适当的类型。 如果您想要将数据写入YAML文件,可以使用以下代码示例: ```cpp #include <iostream> #include <fstream> #include <yaml-cpp/yaml.h> int main() { // 创建一个YAML节点 YAML::Node config; config["name"] = "John"; config["age"] = 25; // 打开要写入的YAML文件 std::ofstream file("output.yaml"); // 将YAML节点写入文件 file << config; return 0; } ``` 在上面的示例,我们创建了一个名为`config`的YAML节点,并设置了两个属性。然后,我们打开了名为`output.yaml`的文件,并使用`<<`操作符将YAML节点写入文件。 这些示例演示了如何使用`yaml-cpp`库在C++读写YAML文件。请确保在编译时链接`yaml-cpp`库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值