读写INI文件源码(点击关注)

适用于INI读取保存的场景,代码交完整,可以编译直接使用;并且作者解决了中文乱码问题;

INI.h

cpp 复制代码
/*
@@此开源代码来自于无锡的钱进,使用者请标注出处
*/
#pragma once
#include <windows.h>
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <locale>
#include <codecvt>
std::string ToString(int value);// Helper function to convert int to string
std::string ToString(float value);// Helper function to convert float to string
std::string ToString(double value);// Helper function to convert double to string
std::string ToString(bool value);// Helper function to convert bool to string
int ToInt(const std::string& str);// Helper function to convert string to int
float ToFloat(const std::string& str);// Helper function to convert string to float
double ToDouble(const std::string& str);// Helper function to convert string to double
bool ToBool(const std::string& str);// Helper function to convert string to bool
std::wstring StringToWString(const std::string& str);// Helper function to convert std::string to wstring
std::string WStringToString(const std::wstring& wstr);
CString stringToCString(const std::string& str);
void SaveIniFile(const std::wstring& section, const std::wstring& key, const std::wstring& value, const std::wstring& filePath);
std::wstring ReadIniFile(const std::wstring& section, const std::wstring& key, const std::wstring& filePath);

INI.cpp

cpp 复制代码
/*
@@此开源代码来自于无锡的钱进,使用者请标注出处
*/
#include"stdafx.h"
#include"INI.h"

std::string ToString(int value) {
	std::ostringstream oss;
	oss << value;
	return oss.str();
}

std::string ToString(float value) {
	std::ostringstream oss;
	oss << value;
	return oss.str();
}

std::string ToString(double value) {
	std::ostringstream oss;
	oss << value;
	return oss.str();
}

std::string ToString(bool value) {
	return value ? "true" : "false";
}

int ToInt(const std::string& str) {
	return std::atoi(str.c_str());
}

float ToFloat(const std::string& str) {
	return std::atof(str.c_str());
}

double ToDouble(const std::string& str) {
	return std::atof(str.c_str());
}

bool ToBool(const std::string& str) {
	return str == "true";
}

// 解决中文乱码问题
std::wstring StringToWString(const std::string& str) {	
	if (str.empty()) {
		return std::wstring();
	}
	std::wstring result;
	int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), NULL, 0);
	if (len < 0)return result;
	wchar_t* buffer = new wchar_t[len + 1];
	if (buffer == NULL)return result;
	MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.size(), buffer, len);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}

std::string WStringToString(const std::wstring& wstr) {
	std::string result;
	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
	if (len <= 0)return result;
	char* buffer = new char[len + 1];
	if (buffer == NULL)return result;
	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;
	return result;
}

CString stringToCString(const std::string& str)
{
	std::wstring wstr = StringToWString(str);
	CString strTemp = CString(wstr.c_str());
	return strTemp;
}

void SaveIniFile(const std::wstring& section, const std::wstring& key, const std::wstring& value, const std::wstring& filePath) {
	WritePrivateProfileString(section.c_str(), key.c_str(), value.c_str(), filePath.c_str());
}

std::wstring ReadIniFile(const std::wstring& section, const std::wstring& key, const std::wstring& filePath) {
	wchar_t buffer[256];
	GetPrivateProfileString(section.c_str(), key.c_str(), L"Default", buffer, 256, filePath.c_str());
	return std::wstring(buffer);
}
相关推荐
在路上看风景15 小时前
19. 成员初始化列表和初始化对象
c++
zmzb010315 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹16 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥17 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
MZ_ZXD00118 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
A星空12319 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫20 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
会叫的恐龙20 小时前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米60120 小时前
C++顺序表和vector
开发语言·c++·算法
独望漫天星辰20 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++