栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ ini文件

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ ini文件

#QT模式
.h实现

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

CPP实现

#include "QtReadIni.h"
#include 
#include 

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 
#include 
#include 
#include 
#include 

class BoostReadIni
{
public:

	void ReadIni();
	//暂时未查到如何判断如何检查ini文件是否具备该节点
	template 
	int GetValue(boost::property_tree::ptree& m_root_node,std::string key, T& value)
	{
		try
		{
			value = m_root_node.get(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("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("System.ip"), std::string("192.236"));
	m_root_node.put(std::string("System.pwd"), std::string("192"));
	m_root_node.put(std::string("ThirdPart.age"), 23);
	boost::property_tree::ini_parser::write_ini(_FileName, m_root_node);
}

调用

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009087.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号