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

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

  • 在项目应用时常常会涉及一些调参工作,如果项目封装成了.exe或者.dll,那么频繁调参多次编译是一件十分低效的事情,如果代码算法或者逻辑是一定的,那么参数完全可以通过读入配置文件来获取
  • 之前在用C++ - opencv的yaml文件配置参数,但是后边参数导入时会出现异常情况,因此更换成 .txt \ .ini \ .config \ .info等格式,这些均为文本文件读取。

1. yaml的使用

  • 在此处放一个opencv官网的例子吧

1.1 yaml写入

  • 写入代码实现:
#include "opencv2/core.hpp"
#include 
using namespace cv;
int main(int, char** argv)
{
    //创建文件
    FileStorage fs("test.yml", FileStorage::WRITE);
    fs << "frameCount" << 5;
    time_t rawtime; time(&rawtime);
    fs << "calibrationDate" << asctime(localtime(&rawtime));
    Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
    Mat distCoeffs = (Mat_<double>(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 >> j) & 1);
        fs << "]" << "}";
    }
    fs << "]";
    fs.release();
    return 0;
}
  • 写入结果:
%YAML:1.0
frameCount: 5
calibrationDate: "Fri Jun 17 14:09:29 2011\n"
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 
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文件读取

  • 参考:https://linuxcpp.0voice.com/?id=75300
  • 代码实现:
#include 
#include 
#include 
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;
}

C++ 文件操作之配置文件读取_第1张图片

你可能感兴趣的:(C++特性,c++,开发语言,windows,visual,studio)