最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

C++ ini文件

互联网 admin 4浏览 0评论

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();

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)

  1. 暂无评论