C++ ini配置文件的格式及读写操作

C++ ini配置文件的格式及读写操作

1 为什么要使用ini或者其它(例如xml,json)配置文件?
如果我们程序没有任何配置文件时,这样的程序对外是全封闭的,一旦程序需要修改一些参数必须要修改程序代码本身并重新编译,这样很不好,所以要用配置文件,让程序发布后还能根据需要进行必要的配置;配置文件有很多如INI配置文件,XML配置文件,还有就是可以使用系统注册表等。注意:ini的后缀名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。因为ini文件实质就是txt文本文件。

2 ini配置文件的格式
1、INI文件由节、键、值组成。
节:
  [section]
参数(键=值)
  name=value
注解
  注解使用分号表示(;)。在分号后面的文字,直到该行结尾都全部为注解。

1)对节进行说明:
什么是sections ?
所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。

2)对参数进行说明:
INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。

3)对注释内容进行说明:
在INI文件中注释语句是以分号“;”开始的(有些人定义类是由#注释,例如下面的RrConfig)。所有的所有的注释语句不管多长都是独占一行直到结束的,在分号和行结束符之间的所有内容都是被忽略的。

好了,以上就是ini配置文件的格式,是最简单的配置文件了。

3 C++读写ini配置文件
在VC中涉及的函数有如下四种。
1)读取字符串

DWORD GetPrivateProfileString( 
								LPCTSTR lpAppName,        // INI文件中的一个字段名[节名]可以有很多个节名(配置文件的section名)
						 	   	LPCTSTR lpKeyName,        // lpAppName 下的一个键名,也就是里面具体的变量名(配置文件的key名)
 							   	LPCTSTR lpDefault,        // 如果lpReturnedString为空,则把这个变量赋给lpReturnedString
  							   	LPTSTR lpReturnedString,  // 存放键值的指针变量,用于接收INI文件中键值(数据)的接收缓冲区
  						 	   	DWORD nSize,              // lpReturnedString的缓冲区大小
  							   	LPCTSTR lpFileName        // INI文件的路径
							 );

2)读取整型值

UINT GetPrivateProfileInt(
 		LPCTSTR lpAppName,   // INI文件中的一个字段名[节名]可以有很多个节名
 		LPCTSTR lpKeyName,   // lpAppName 下的一个键名,也就是里面具体的变量名
 		INT nDefault,        // 如果没有找到指定的数据返回,则把个变量值赋给返回值
  		LPCTSTR lpFileName   // INI文件的路径
		);

3)写入字符串

BOOL WritePrivateProfileString(
  			LPCTSTR lpAppName,  // INI文件中的一个字段名[节名]可以有很多个节名
  			LPCTSTR lpKeyName,  // lpAppName 下的一个键名,也就是里面具体的变量名
  			LPCTSTR lpString,   // 键值,也就是数据
  			LPCTSTR lpFileName  // INI文件的路径
			);

4)写入整数值(没有相关函数,可以通过WritePrivateProfileString进行参数转换来实现)

4 代码示例
由于VC提供的函数部分功能比较少,一般解析配置文件都是调用库或者别人在项目中使用过的类,所以上面只是为了让大家了解ini的格式及相关参数的意义。下面的代码其实不需要看懂,只需要你会用即可。

ReConfig.h文件

#ifndef RR_CONFIG_H_
#define RR_CONFIG_H_
#include <string>
#include <map>
namespace rr
{
    
    
	class RrConfig
	{
    
    
	public:
		RrConfig()
		{
    
    
		}
		~RrConfig()
		{
    
    
		}
		bool ReadConfig(const std::string & filename);
		std::string ReadString(const char* section, const char* item, const char* default_value);
		int ReadInt(const char* section, const char* item, const int& default_value);
		float ReadFloat(const char* section, const char* item, const float& default_value);
	private:
		bool IsSpace(char c);
		bool IsCommentChar(char c);
		void Trim(std::string & str);
		bool AnalyseLine(const std::string & line, std::string& section, std::string & key, std::string & value);

	private:
		//std::map<std::string, std::string> settings_;
		std::map<std::string, std::map<std::string, std::string> >settings_;
	};
}
#endif

ReConfig.cpp文件

#include "RrConfig.h"
#include <fstream>
#include <stdlib.h>

namespace rr
{
    
    

	bool RrConfig::IsSpace(char c)
	{
    
    
		if (' ' == c || '\t' == c)
			return true;
		return false;
	}

	bool RrConfig::IsCommentChar(char c)
	{
    
    
		switch (c) {
    
    
		case '#':
			return true;
		default:
			return false;
		}
	}

	void RrConfig::Trim(std::string & str)
	{
    
    
		if (str.empty())
		{
    
    
			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 RrConfig::AnalyseLine(const std::string & line, std::string& section, std::string & key, std::string & value)
	{
    
    
		if (line.empty())
			return false;
		int start_pos = 0, end_pos = line.size() - 1, pos, s_startpos, s_endpos;
		if ((pos = line.find("#")) != -1)
		{
    
    
			if (0 == pos)
			{
    
    
				return false;
			}
			end_pos = pos - 1;
		}
		if (((s_startpos = line.find("[")) != -1) && ((s_endpos = line.find("]"))) != -1)
		{
    
    
			section = line.substr(s_startpos + 1, s_endpos - 1);
			return true;
		}
		std::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);
		value = new_line.substr(pos + 1, end_pos + 1 - (pos + 1));
		Trim(key);
		if (key.empty()) {
    
    
			return false;
		}
		Trim(value);
		if ((pos = value.find("\r")) > 0)
		{
    
    
			value.replace(pos, 1, "");
		}
		if ((pos = value.find("\n")) > 0)
		{
    
    
			value.replace(pos, 1, "");
		}
		return true;
	}

	bool RrConfig::ReadConfig(const std::string & filename)
	{
    
    
		settings_.clear();
		std::ifstream infile(filename.c_str());//构造默认调用open,所以可以不调用open
		//std::ifstream infile;
		//infile.open(filename.c_str());
		//bool ret = infile.is_open()
		if (!infile) {
    
    
			return false;
		}
		std::string line, key, value, section;
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		while (getline(infile, line))
		{
    
    
			if (AnalyseLine(line, section, key, value))
			{
    
    
				it = settings_.find(section);
				if (it != settings_.end())
				{
    
    
					k_v[key] = value;
					it->second = k_v;
				}
				else
				{
    
    
					k_v.clear();
					settings_.insert(std::make_pair(section, k_v));
				}
			}
			key.clear();
			value.clear();
		}
		infile.close();
		return true;
	}

	std::string RrConfig::ReadString(const char* section, const char* item, const char* default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::string def(default_value);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			//printf("111");
			return def;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			//printf("222");
			return def;
		}
		return it_item->second;
	}

	int RrConfig::ReadInt(const char* section, const char* item, const int& default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			return default_value;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			return default_value;
		}
		return atoi(it_item->second.c_str());
	}

	float RrConfig::ReadFloat(const char* section, const char* item, const float& default_value)
	{
    
    
		std::string tmp_s(section);
		std::string tmp_i(item);
		std::map<std::string, std::string> k_v;
		std::map<std::string, std::string>::iterator it_item;
		std::map<std::string, std::map<std::string, std::string> >::iterator it;
		it = settings_.find(tmp_s);
		if (it == settings_.end())
		{
    
    
			return default_value;
		}
		k_v = it->second;
		it_item = k_v.find(tmp_i);
		if (it_item == k_v.end())
		{
    
    
			return default_value;
		}
		return atof(it_item->second.c_str());
	}
}

测试main.cpp
前提是首先在你想要的路径下配置一个.ini文件,我的是config.ini。

#include <iostream>
#include "RrConfig.h"
#include <fstream>
#include <cassert>

int main() {
    
    

	rr::RrConfig config;
	bool ret = config.ReadConfig("config.ini.txt");
	if (ret == false) {
    
    
		printf("ReadConfig is Error,Cfg=%s", "config.ini");
		return 1;
	}
	std::string HostName = config.ReadString("MYSQL", "HostName", "");
	int Port = config.ReadInt("MYSQL", "Port", 0);
	std::string UserName = config.ReadString("MYSQL", "UserName", "");

	std::cout << "HostName=" << HostName << std::endl;
	std::cout << "Port=" << Port << std::endl;
	std::cout << "UserName=" << UserName << std::endl;

	return 0;
}

ini文件内容:
在这里插入图片描述

结果:
在这里插入图片描述

5 总结
在测试的时候遇到了一个问题,其实之前很早也遇到过,就是如果打开的文件不带上.txt的话,文件无法打开,一直返回false,不知道你们是否会遇到这种问题,这里也是记录一下避免大家浪费时间。
看完这篇后,就能够帮你解决掉对ini配置的项目需求。

//bool ret = config.ReadConfig("config.ini");//error
bool ret = config.ReadConfig("config.ini.txt");

6 配置文件的解析库
1)libconfig, C++版本是libconfig++
libconfig第一篇———使用指南
http://blog.csdn.net/crazyhacking/article/details/9668981

2)C++的Json解析库:jsoncpp和boost .
http://www.cnblogs.com/lidabo/archive/2012/10/31/2748026.html
由于项目中暂时未对json要求,这里只是记录一下,以后有机会再玩玩。

猜你喜欢

转载自blog.csdn.net/weixin_44517656/article/details/109014236