Ini 文件读写之C++

这篇博客介绍了如何使用C++进行Ini配置文件的读写操作,详细阐述了Ini文件格式,并提供了完整的源码及测试代码示例,帮助读者理解和应用 Ini 文件的读写功能。
摘要由CSDN通过智能技术生成

参考来源:https://blog.csdn.net/m_buddy/article/details/54097131

目录

Ini文件格式

源码

测试源码 

测试结果


Ini文件格式

源码

//IniFile.h
#ifndef INIFILE_H_
#define INIFILE_H_
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
using std::string;
using std::vector;
using std::map;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::cout;
using std::stringstream;

//ini节点
class CSection
{
public:
	CSection(const string& section, const string&  key, const string&  value);
	string m_section;
	string m_key;
	string m_value;
};

//ini 键 值 对
class CKeyValue
{
public:
	void AddKeyValue(const string&  key, const string&  value);
	map<string, string> m_keyValue;
};

class CIniFile
{
public:
	CIniFile();
	~CIniFile();
	bool LoadFromIni(const string&  filename);
	vector<CSection>::size_type SetValue(const string&  section, const string&  key, const string&  value);
	map<string, CKeyValue>::size_type GetSize();
	bool SaveToIni(const string&  path);
	string GetValue(const string&  section, const string&  key);
	void StrToInt(const string&  str, int& num);
	void StrToDouble(const string&  str, double& num);
	void strToChar(const string&  str, char& num);
	string FolderPath();

	//返回一个不含strOld
	string strReplaceAll(const string& strResource, const string& strOld, const string& strNew);

	//去首尾空格
	string& Trim(string& str);
	
private:
	map<string, CKeyValue> m_secKeyValue;
};
#endif
//IniFile.cpp
#include "stdafx.h"
#include <windows.h>
#include "IniFile.h"


CSection::CSection(const stri
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值