C++读取ini配置⽂件
读取ini⽂件,创建⼯具类
⽰例代码
CParseIniFile.h
#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <map>
using namespace std;
深圳西乡沃尔玛#define COMMENT_CHAR '#'
class CParseIniFile
{
public:
CParseIniFile();
~CParseIniFile();
bool ReadConfig(const string& filename, map<string, string>& mContent, const char* section);
bool AnalyseLine(const string & line, string & key, string & val);
void Trim(string & str);
女生就业前景好的行业bool IsSpace(char c);
bool IsCommentChar(char c);
void PrintConfig(const map<string, string> & mContent);
private:
};
CParseIniFile.cpp
#include "CParseIniFile.h"
CParseIniFile::CParseIniFile()
{
}
CParseIniFile::~CParseIniFile()
{
}
bool CParseIniFile::ReadConfig(const string& filename, map<string, string>& mContent, const char* section) {
mContent.clear();
ifstream infile(filename.c_str());
if (!infile)
{
//LOG4CXX_ERROR(logger, "file open error!");
return false;
}
string line, key, value;
int pos = 0;
string Tsection = string("[") + section + "]";
bool flag = false;
while (getline(infile, line))
{
pos = line.find(Tsection, 0);
if (-1 == pos)
{
continue;
}
else黄海波被谁害得
{
flag = true;
getline(infile, line);
读取配置文件失败
}
}
if (0 < line.length() && '[' == line.at(0))
{
break;
}
if (0 < line.length() && AnalyseLine(line, key, value))
{
if (value.length() > 0)
{
if (value[value.size() - 1] == '\r')
{
value[value.size() - 1] = '\0';
}
}
mContent[key] = value;
}
}
infile.close();
return true;
}
bool CParseIniFile::AnalyseLine(const string & line, string & key, string & val)
{
if (pty())
{
return false;
}
int start_pos = 0, end_pos = line.size() - 1, pos = 0;
if ((pos = line.find(COMMENT_CHAR)) != -1)
{
if (0 == pos)
{//⾏的第⼀个字符就是注释字符
return false;
}
end_pos = pos - 1;
}
string new_line = line.substr(start_pos, start_pos + 1 - end_pos); // 预处理,删除注释部分
if ((pos = new_line.find('=')) == -1)
{
return false; // 没有=号
}
key = new_line.substr(0, pos);
val = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
Trim(key);
if (pty())
{
return false;
}
Trim(val);
void CParseIniFile::Trim(string & str)
{
if (pty())
{
return;
}
int i, start_pos, end_pos;
for (i = 0; i < str.size(); ++i)
{
if (!IsSpace(str[i]))
{
break;
}
}
if (i == str.size())
{ //全部是空⽩字符串
str = "";
return;
}
start_pos = i;
for (i = str.size() - 1; i >= 0; --i)
{
if (!IsSpace(str[i]))
{
break;
}
}
end_pos = i;
str = str.substr(start_pos, end_pos - start_pos + 1);
}
bool CParseIniFile::IsSpace(char c)
{
if (' ' == c || '\t' == c)
{
return true;
}
return false;
}
闫妮刮阴门bool CParseIniFile::IsCommentChar(char c)
{
switch (c)
{
case COMMENT_CHAR:
return true;
中国好声音王拓default:
return false;
}
}
void CParseIniFile::PrintConfig(const map<string, string> & mContent) {
map<string, string>::const_iterator mite = mContent.begin();
for (; mite != d(); ++mite)
{
cout << mite->first << "=" << mite->second << endl;
}
}
调⽤⽂件:main.cpp
#include <stdio.h>
#include <iostream>
#include "CParseIniFile.h"
int main()
{
/
/string fileName;
//string section = "finish_model_name";
map<string,string>fname;
CParseIniFile config;
bool flage=config.ReadConfig("config.ini",fname,"COMMOND"); if (flage)
{
cout << fname["finish_model_name"] << endl;
cout << fname["client_model_name"] << endl;
cout << stod(fname["threshold"]) << endl;
}
//config.PrintConfig(fname);
}
发布评论