c++读写ini配置文件数据类

本文介绍了一个用于解析和操作配置文件的C++类CConfigOperator,该类能够读取、修改配置文件中的值,并提供了将字符串转换为向量的功能。通过实例展示了如何使用该类获取配置参数,如机械臂的IP地址、端口号、最大速度和加速度,以及路点变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类的头文件:

#ifndef CONFIGCLASS_H
#define CONFIGCLASS_H

#include<iostream>
#include <fstream>
#include <vector>
#include<string>
#include <sstream>
#include <cstring>
using namespace std;
class CConfigOperator
{
public:
 CConfigOperator(void);
 ~CConfigOperator(void);
 void SetFilePath(const string &sFilePath);
 string GetFilePath();
 bool GetConfigValue(const string &sName,const string &skey,string &sValue,string &sError);
 bool ModefyConfigValue(const string &sName,const string &skey,const string &sValue,string &sError);
 vector<double> TransStrToVec(string str);
private:
 bool OpenFile();
 void FindName();
 bool FindKey();
 bool WriteFile();
 bool ModefyValue();
 void WriteToFile(vector<string> &vContent);
 fstream m_fout;
 ifstream m_fin;
 string m_sFilePath;
 string m_Name;
 string m_Key;
 string m_value;
 string m_sError;
 string m_sStr;
 bool m_bFindName;
};

#endif // CONFIGCLASS_H

类的源文件

#include "configclass.h"

CConfigOperator::CConfigOperator(void)
{
}
CConfigOperator::~CConfigOperator(void)
{
}
/************************************
 设置配置文件路径
************************************/
void CConfigOperator::SetFilePath(const string &sFilePath)
{
 m_sFilePath = sFilePath;
}

/************************************
 得到配置文件路径
************************************/
string CConfigOperator::GetFilePath()
{
 return this->m_sFilePath;
}

/************************************
  打开配置文件
************************************/
bool CConfigOperator:: OpenFile()
{
 if(true == m_fin.is_open())
 {
  m_fin.close();
 }
 m_fin.open(m_sFilePath.c_str());
 if(NULL == m_fin)
 {
  m_sError = "can not open file " +m_sFilePath;
  return false;
 }
 return true;
}
/************************************
  查找配置文件的名字
************************************/
void CConfigOperator::FindName()
{
 if(-1 != m_sStr.find('['))
 {
  string sTemp = m_sStr.substr(m_sStr.find('[') + 1,m_sStr.find(']') - m_sStr.find('[') - 1);
  if(0 == strcmp(sTemp.c_str(),m_Name.c_str()))
  {
   m_bFindName = true;
   m_sError = "Find Name But Not Find Key";
  }
  else
  {
   m_bFindName = false;
  }
 }
}
/************************************
  查找配置文件的Key
************************************/
bool  CConfigOperator::FindKey()
{
 int iDelePlace = m_sStr.find('//');
 int iFindEqual = m_sStr.find('=');
    //被注释的行,或者是包含key但是已经被注视掉了,过滤
 if( (- 1 != iDelePlace && iDelePlace <  iFindEqual) || (- 1 != iDelePlace && -1 == iFindEqual) ||  -1 == iFindEqual )
 {
  return false;
 }
 string sKey = m_sStr.substr(0,m_sStr.find('='));

 if(0 == strcmp(sKey.c_str(),m_Key.c_str()))
 {
  m_value = m_sStr.substr(m_sStr.find('=') + 1 , m_sStr.length() - m_sStr.find('=') - 1);
  return true;
 }

 return false;
}

/************************************
 读取配置文件NEMA KEY 对应的Value信息
************************************/
bool CConfigOperator::GetConfigValue(const string &sName,const string &skey,string &sValue,string &sError)
{
  m_sError = "";
  m_Name = sName;
  m_Key = skey;
 if(false == OpenFile())
 {
  sError = m_sError;
  return false;
 }
 char str[1024];
 m_bFindName = false;
 while(NULL != m_fin.getline(str,sizeof(str)))
 {
  m_sStr = str;
  FindName();
  if(true == m_bFindName)
  {
   if(true == FindKey())
   {
    m_fin.close();
    sError = "";
    sValue = m_value;
    return true;
   }
  }

 }
 sError = m_sError;
 m_fin.close();
 return false;
}
/************************************
  写的方式打开文件
************************************/
bool CConfigOperator::WriteFile()
{
 m_fout.close();
 //关闭后要在清空一下,否则下次打开会出错
 m_fout.clear();
 m_fout.open(m_sFilePath.c_str(),ios::in|ios::out);
 if(NULL == m_fout)
 {
  m_sError = "can not open file " +m_sFilePath;
  return false;
 }
 return true;
}

/************************************
 修改配置文件Key对应的值
************************************/
bool  CConfigOperator::ModefyValue()
{
 int iDelePlace = m_sStr.find('//');
 int iFindEqual = m_sStr.find('=');
    //被注释的行,或者是包含key但是已经被注视掉了,过滤
 if( (- 1 != iDelePlace && iDelePlace <  iFindEqual) || (- 1 != iDelePlace && -1 == iFindEqual) ||  -1 == iFindEqual )
 {
  return false;
 }
 string sKey = m_sStr.substr(0,m_sStr.find('='));

 if(0 == strcmp(sKey.c_str(),m_Key.c_str()))
 {
  m_sStr =  m_sStr.substr(0,m_sStr.find('=') + 1) + m_value ;
  return true;
 }

 return false;
}
/************************************
修改后的配置文件信息重新写入文件
************************************/
void CConfigOperator::WriteToFile(vector<string> &vContent)
{
 if(false == WriteFile())
 {
  m_fout.close();
  return;
 }
 for(size_t iIndex = 0; iIndex < vContent.size(); iIndex ++)
 {
  m_fout<<vContent[iIndex]<<endl;
 }
 m_fout.close();
 vector<string>().swap(vContent);
}
/************************************
 修改配置文件NEMA KEY 对应的Value信息
************************************/
bool CConfigOperator::ModefyConfigValue(const string &sName,const string &skey,const string &sValue,string &sError)
{
  m_sError = "";
  m_Name = sName;
  m_Key = skey;
  m_value = sValue;
 if(false == WriteFile())
 {
  sError = m_sError;
  return false;
 }
 char str[1024];
 m_bFindName = false;
 vector<string> vContent;
 bool isModefy = false;
 while(NULL != m_fout.getline(str,sizeof(str)))
 {
  m_sStr = str;

  FindName();
  if(true == m_bFindName)
  {
   if(true == ModefyValue())
   {
    isModefy =  true;
   }
  }

  vContent.push_back(m_sStr);
 }
 sError = m_sError;
 WriteToFile(vContent);
 m_fout.close();
 return isModefy;
}

vector<double> CConfigOperator::TransStrToVec(string str){
    stringstream ss(str);
    string buff;
    vector<double> arr;

    while (ss >> buff)
            arr.push_back(atof(buff.c_str()));

    return arr;
}

配置文件cfg.ini文件放在编译生成的二进制文件目录下,内容如下

[config]

robot_host=127.0.0.1
robot_port=8899

;m/s
maxvel=0.1
maxacc=0.1
;deg/s
maxdq=30
maxdqq=10

[waypoint]
;deg
lefthome=10 20 30 40 50 60
righthome=0.1 0.2 0.3 0.4 0.5 0.6
box1quleft=15 25 35 45 55 65
box8quright=0.1 0.2 0.3 0.4 0.5 0.6

类的使用示例

/**打开配置文件**/
    CConfigOperator tCConfigOperator;
    string sFielPath = "cfg.ini";
    tCConfigOperator.SetFilePath(sFielPath);
    string sName = "";
    string sKey = "";
    string sValue = "";
    string sEroor = "";
    //tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    //cout<<"valuse is"<<sValue<<" sEroor is"<<sEroor<<endl;

    /**获取机械臂的ip和控制端口**/
    sName = "config";
    sKey = "robot_host";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    const char* SERVER_HOST = sValue.c_str();
    sKey = "robot_port";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    int SERVER_PORT = atoi(sValue.c_str());

    /**获取最大加速度和速度值**/
    sKey = "maxvel";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    double maxvel = atof(sValue.c_str());
    sKey = "maxacc";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    double maxacc = atof(sValue.c_str());
    sKey = "maxdq";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    double maxdq = atof(sValue.c_str());
    sKey = "maxdqq";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    double maxdqq = atof(sValue.c_str());

    /**获取路点变量**/
    sName = "waypoint";
    sKey = "lefthome";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    vector<double> point = tCConfigOperator.TransStrToVec(sValue);
    Util::initJointAngleArray(lefthomepoint, point[0]/180.0*M_PI, point[1]/180.0*M_PI, point[2]/180.0*M_PI,
            point[3]/180.0*M_PI, point[4]/180.0*M_PI, point[5]/180.0*M_PI);

    sKey = "righthome";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    point = tCConfigOperator.TransStrToVec(sValue);
    Util::initJointAngleArray(righthomepoint, point[0]/180.0*M_PI, point[1]/180.0*M_PI, point[2]/180.0*M_PI,
            point[3]/180.0*M_PI, point[4]/180.0*M_PI, point[5]/180.0*M_PI);

    sKey = "box1quleft";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    point = tCConfigOperator.TransStrToVec(sValue);
    Util::initJointAngleArray(box1quleftpoint, point[0]/180.0*M_PI, point[1]/180.0*M_PI, point[2]/180.0*M_PI,
            point[3]/180.0*M_PI, point[4]/180.0*M_PI, point[5]/180.0*M_PI);

    sKey = "box8quright";
    tCConfigOperator.GetConfigValue(sName,sKey,sValue,sEroor);
    point = tCConfigOperator.TransStrToVec(sValue);
    Util::initJointAngleArray(box8qurightpoint, point[0]/180.0*M_PI, point[1]/180.0*M_PI, point[2]/180.0*M_PI,
            point[3]/180.0*M_PI, point[4]/180.0*M_PI, point[5]/180.0*M_PI);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值