c++读写ini配置⽂件数据类类的头⽂件:
#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;
}杨冰胖丫
/************************************
读取配置⽂件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 != line(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 ) {
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 != line(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();
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
类的使⽤⽰例
发布评论