C++ 文件操作配置文件读取

1. yaml使用

1.1 yaml写入

#include "opencv2/core.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{
    //创建文件
    FileStorage fs("test.yml", FileStorage::WRITE);
    fs &lt;&lt; "frameCount" << 5;
    time_t rawtime; time(&amp;rawtime);
    fs << "calibrationDate" << asctime(localtime(&amp;rawtime));
    Mat cameraMatrix = (Mat_<double&gt;(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
    Mat distCoeffs = (Mat_<double&gt;(5,1) << 0.1, 0.01, -0.001, 0, 0);
    fs << "cameraMatrix" << cameraMatrix << "distCoeffs" << distCoeffs;
    fs << "features" << "[";
    for( int i = 0; i < 3; i++ )
    {
        int x = rand() % 640;
        int y = rand() % 480;
        uchar lbp = rand() % 256;
        fs << "{:" << "x" << x << "y" << y << "lbp" << "[:";
        for( int j = 0; j < 8; j++ )
            fs << ((lbp &gt;&gt; j) &amp; 1);
        fs << "]" << "}";
    }
    fs << "]";
    fs.release();
    return 0;
}
%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011n"
cameraMatrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 1000., 0., 320., 0., 1000., 240., 0., 0., 1. ]
distCoeffs: !!opencv-matrix
   rows: 5
   cols: 1
   dt: d
   data: [ 1.0000000000000001e-01, 1.0000000000000000e-02,
       -1.0000000000000000e-03, 0., 0. ]
features:
   - { x:167, y:49, lbp:[ 1, 0, 0, 1, 1, 0, 1, 1 ] }
   - { x:298, y:130, lbp:[ 0, 0, 0, 1, 0, 0, 1, 1 ] }
   - { x:344, y:158, lbp:[ 1, 1, 0, 0, 0, 0, 1, 0 ] }

1.2 yaml读取

#include "opencv2/core.hpp"
#include <time.h>
using namespace cv;
int main(int, char** argv)
{    
    FileStorage fs2("test.yml", FileStorage::READ);
    
    //注意数据格式转换
    // first method: use (type) operator on FileNode.
    int frameCount = (int)fs2["frameCount"];
    String date;
    // second method: use FileNode::operator >>
    fs2["calibrationDate"] >> date;
    Mat cameraMatrix2, distCoeffs2;
    fs2["cameraMatrix"] >> cameraMatrix2;
    fs2["distCoeffs"] >> distCoeffs2;
    cout << "frameCount: " << frameCount << endl
         << "calibration date: " << date << endl
         << "camera matrix: " << cameraMatrix2 << endl
         << "distortion coeffs: " << distCoeffs2 << endl;
    FileNode features = fs2["features"];
    FileNodeIterator it = features.begin(), it_end = features.end();
    int idx = 0;
    std::vector<uchar> lbpval;
    // iterate through a sequence using FileNodeIterator
    for( ; it != it_end; ++it, idx++ )
    {
        cout << "feature #" << idx << ": ";
        cout << "x=" << (int)(*it)["x"] << ", y=" << (int)(*it)["y"] << ", lbp: (";
        // you can also easily read numerical arrays using FileNode >> std::vector operator.
        (*it)["lbp"] >> lbpval;
        for( int i = 0; i < (int)lbpval.size(); i++ )
            cout << " " << (int)lbpval[i];
        cout << ")" << endl;
    }
    fs2.release();
    return 0;
}

2. ini文件读取

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    // 打开配置文件
    ifstream config_file("config.txt");
    if (!config_file.is_open())
    {
        cerr << "Failed to open config file." << endl;
        return -1;
    }
    // 逐行读取并解析数据
    string line;
    while (getline(config_file, line))
    {
        // 如果是注释或空行,则忽略
        if (line.empty() || line[0] == '#')
            continue;
        // 解析key-value
        size_t pos = line.find('=');
        if (pos != string::npos)
        {
            string key = line.substr(0, pos);
            string value = line.substr(pos + 1);
            cout << "Key: " << key << ", Value: " << value << endl;
        }
    }
    // 关闭文件
    config_file.close();
    return 0;
}

在这里插入图片描述

原文地址:https://blog.csdn.net/yohnyang/article/details/134794466

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_43272.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注