C++ ini文件

#QT模式
.h实现

#include <QtCore/QString>
class  QtReadIni
{
public:
	//读取
	virtual void ReadConstruction();
	//保存
	virtual void SaveConstruction();
};

CPP实现

#include "QtReadIni.h"
#include <QtCore/QSettings>
#include <iostream>

void QtReadIni::ReadConstruction()
{
	QSettings* IniFile = new QSettings(QString("Config.ini"), QSettings::IniFormat);
	if (IniFile)
	{

		QString GroupName = "System/ip";
		//判断该ini文件中是否有该节点
		bool Key = IniFile->contains(GroupName);
		if (Key == true)
		{
			 QString  USETimeCtrl = IniFile->value(GroupName).toString();
		}
		 GroupName = "System/pwd";
		bool Key2 = IniFile->contains(GroupName);
		if (Key == true)
		{
			int  USETimeCtrl = IniFile->value(GroupName).toInt();	
		}
	
		 GroupName = "ThirdPart/age";
		bool Key3 = IniFile->contains(GroupName);
		if (Key == true)
		{
			int  USETimeCtrl = IniFile->value(GroupName).toInt();
		}

		delete IniFile;
	}
}

void QtReadIni::SaveConstruction()
{
	QSettings* IniFile = new QSettings( QString("Config.ini"), QSettings::IniFormat);
	QString GroupName = "System";
	IniFile->beginGroup(GroupName);
	IniFile->setValue(QString("ip"), QString("127.0.0.1"));
	IniFile->setValue(QString("pwd"), ("123456"));
	IniFile->endGroup();
	GroupName = "ThirdPart";
	IniFile->beginGroup(GroupName);
	IniFile->setValue(QString("age"), 22);
	IniFile->endGroup();
	delete IniFile;
}

调用

	QtReadIni IniR;
	IniR.SaveConstruction();
	IniR.Updateini();
	return 0;

结果
在这里插入图片描述

Boost读取ini

头文件

#pragma once
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <iostream>

class BoostReadIni
{
public:

	void ReadIni();
	//暂时未查到如何判断如何检查ini文件是否具备该节点
	template <typename T>
	int GetValue(boost::property_tree::ptree& m_root_node,std::string key, T& value)
	{
		try
		{
			value = m_root_node.get<T>(key);
		}
		catch (std::exception e)
		{
			std::cerr << e.what() << std::endl;
			return false;
		}
		return true;
	}

	bool UpdateItem();

	void SaveFile();
private:

};

CPP实现

#include "BoostReadIni.h"


void BoostReadIni::ReadIni()
{
	std::string _FileName = "Config.ini";
	//判断是否具备该文件
	if (boost::filesystem::exists(_FileName))
	{
		boost::property_tree::ptree m_root_node;
		boost::property_tree::ini_parser::read_ini(_FileName, m_root_node);
		std::string strIP;
		if (GetValue(m_root_node,"System.ip", strIP))
		{
			std::cout << strIP << std::endl;
		}
		std::string strIPVE;
		if (GetValue(m_root_node, "System.VE", strIPVE))
		{
			std::cout << strIP << std::endl;
		}
		else
		{
			std::cout << "获取改值失败" << std::endl;
		}
		int iAge;
		if (GetValue(m_root_node, "ThirdPart.age", iAge))
		{
			std::cout << iAge << std::endl;
		}
		

	
	}
}

bool BoostReadIni::UpdateItem()
{
	std::string _FileName = "Config.ini";
	if (boost::filesystem::exists(_FileName))
	{
		boost::property_tree::ptree m_root_node;
		boost::property_tree::ini_parser::read_ini(_FileName, m_root_node);
		m_root_node.put<std::string>(std::string("System.ip"), std::string("192"));
		boost::property_tree::ini_parser::write_ini(_FileName, m_root_node);
		return true;
	}
	return false;
}

void BoostReadIni::SaveFile()
{
	std::string _FileName = "Config.ini";
	boost::property_tree::ptree m_root_node;
	if (boost::filesystem::exists(_FileName))
	{
		boost::property_tree::ini_parser::read_ini(_FileName, m_root_node);
	}
	
	m_root_node.put<std::string>(std::string("System.ip"), std::string("192.236"));
	m_root_node.put<std::string>(std::string("System.pwd"), std::string("192"));
	m_root_node.put<int>(std::string("ThirdPart.age"), 23);
	boost::property_tree::ini_parser::write_ini(_FileName, m_root_node);
}

调用

		BoostReadIni config;
		config.SaveFile();
		
		config.UpdateItem();
		config.ReadIni();

在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值