当前位置: 首页 > news >正文

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

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

INI.h

/*
@@此开源代码来自于无锡的钱进,使用者请标注出处
*/
#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

/*
@@此开源代码来自于无锡的钱进,使用者请标注出处
*/
#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);
}

http://www.dtcms.com/a/458838.html

相关文章:

  • ps如何做网站轮播图网站开发通过什么途径接活
  • 如何选择网站开发公司鲜花网站建设的利息分析
  • 可做生物试卷的网站网站建设多少钱兴田德润放心
  • 做网站要有策划么江都微信网站建设
  • 网站模版 模板门户网站系统有哪些平台
  • 深圳建科技有限公司网站首页wordpress投稿页面
  • 南昌网站开发培训学校设计制作生态瓶
  • 合肥房产网站建设建设网站的申请信用卡
  • 鲁中网站wordpress+park主题
  • modbus tcp 跟 modbus rtu
  • NVMe高速传输之摆脱XDMA设计43:队列管理功能验证与分析1
  • 网站开发课程软件wordpress幻灯片主题设置
  • 域名过期网站还有用吗营销型网站的推广方法
  • C++(10.4)
  • 2018什么做网站简易网页模板
  • 果洛州商城网站建设分析网站建设的体会
  • 网站更新问题怀化市建设局门户网站
  • 双耳听觉与空间声感知的实验原理---笔记
  • 网站建设需要什么流程免费自取ppt模板
  • 怎样制作网站后台网络推广培训哪个好
  • 洛阳做网站优化最新新闻热点事件50字
  • 重庆玖玺国际做网站做暧免费网站
  • MySQL包安装 -- SUSE系列(SUSE资源库安装MySQL)
  • 从零开始的C语言学习014 指针5
  • photoshop做图网站长春建设集团股份有限公司网站
  • 都江堰旅游门户网站电子商务网站的开发语言
  • 卡片式设计的网站旅游网站建设系统
  • 做铁合金用哪个外贸网站好烟台网站建设的方法有哪些
  • 网站建设需要哪些企业资料网络营销有哪些策略
  • 网站开发的调研内容今天的新闻联播直播在线观看