惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Latest news
Latest news
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
V
V2EX
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
Cisco Talos Blog
Cisco Talos Blog
K
Kaspersky official blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
小众软件
小众软件
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
腾讯CDC
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
G
GRAHAM CLULEY
罗磊的独立博客
T
Tor Project blog
C
Cisco Blogs
美团技术团队
博客园 - Franky
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Cyberwarzone
Cyberwarzone
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Security Latest
Security Latest
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
Spread Privacy
Spread Privacy
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
阮一峰的网络日志
阮一峰的网络日志
雷峰网
雷峰网
Project Zero
Project Zero

博客园 - arvin2012

VC++常用方法函数集合, EVC++4.0 长用数据类型转换,FFT算法 指向函数的指针数组到底该怎么用 内存耗尽怎么办? 位运算符的一些简单应用 异步Socket通信总结 贵在坚持 vc++操作word MFC中的ClassWizard的使用 追MM与设计模式的有趣见解 动软.Net代码生成器 发布最新2.12版 vc中常用的方法 vc中读写注册表 c++ file and directory 删除,移动,目录浏览对话框,找某目录下的所有文件(包括子目录) Windows API 技巧集 vc 使用总结 VC 常见的108个问题 VC中动态生成控件 什么是COM,如何使用COM
简单好用的读写ini文件的类
arvin2012 · 2008-07-28 · via 博客园 - arvin2012

IniReader.h

#ifndef INIREADER_H
#define INIREADER_H
class CIniReader
{
public:
 CIniReader(
char* szFileName); 
 
int ReadInteger(char* szSection, char* szKey, int iDefaultValue);
 
float ReadFloat(char* szSection, char* szKey, float fltDefaultValue);
 
bool ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue);
 
char* ReadString(char* szSection, char* szKey, const char* szDefaultValue);
private:
  
char m_szFileName[255];
}
;
#endif//INIREADER_H

IniReader.cpp

#include "IniReader.h"
#include 
<iostream>
#include 
<Windows.h>
CIniReader::CIniReader(
char* szFileName)
{
 memset(m_szFileName, 
0x00255);
 memcpy(m_szFileName, szFileName, strlen(szFileName));
}

int CIniReader::ReadInteger(char* szSection, char* szKey, int iDefaultValue)
{
 
int iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, m_szFileName); 
 
return iResult;
}

float CIniReader::ReadFloat(char* szSection, char* szKey, float fltDefaultValue)
{
 
char szResult[255];
 
char szDefault[255];
 
float fltResult;
 sprintf(szDefault, 
"%f",fltDefaultValue);
 GetPrivateProfileString(szSection,  szKey, szDefault, szResult, 
255, m_szFileName); 
 fltResult 
=  atof(szResult);
 
return fltResult;
}

bool CIniReader::ReadBoolean(char* szSection, char* szKey, bool bolDefaultValue)
{
 
char szResult[255];
 
char szDefault[255];
 
bool bolResult;
 sprintf(szDefault, 
"%s", bolDefaultValue? "True" : "False");
 GetPrivateProfileString(szSection, szKey, szDefault, szResult, 
255, m_szFileName); 
 bolResult 
=  (strcmp(szResult, "True"== 0 || strcmp(szResult, "true"== 0? true : false;
 
return bolResult;
}

char* CIniReader::ReadString(char* szSection, char* szKey, const char* szDefaultValue)
{
 
char* szResult = new char[255];
 memset(szResult, 
0x00255);
 GetPrivateProfileString(szSection,  szKey, szDefaultValue, szResult, 
255, m_szFileName); 
 
return szResult;
}

IniWriter.h

#ifndef INIWRITER_H
#define INIWRITER_H
class CIniWriter
{
public:
 CIniWriter(
char* szFileName); 
 
void WriteInteger(char* szSection, char* szKey, int iValue);
 
void WriteFloat(char* szSection, char* szKey, float fltValue);
 
void WriteBoolean(char* szSection, char* szKey, bool bolValue);
 
void WriteString(char* szSection, char* szKey, char* szValue);
private:
 
char m_szFileName[255];
}
;
#endif //INIWRITER_H

IniWriter.cpp

#include "IniWriter.h"
#include 
<iostream>
#include 
<Windows.h> 
CIniWriter::CIniWriter(
char* szFileName)
{
 memset(m_szFileName, 
0x00255);
 memcpy(m_szFileName, szFileName, strlen(szFileName));
}

void CIniWriter::WriteInteger(char* szSection, char* szKey, int iValue)
{
 
char szValue[255];
 sprintf(szValue, 
"%d", iValue);
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}

void CIniWriter::WriteFloat(char* szSection, char* szKey, float fltValue)
{
 
char szValue[255];
 sprintf(szValue, 
"%f", fltValue);
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}

void CIniWriter::WriteBoolean(char* szSection, char* szKey, bool bolValue)
{
 
char szValue[255];
 sprintf(szValue, 
"%s", bolValue ? "True" : "False");
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName); 
}

void CIniWriter::WriteString(char* szSection, char* szKey, char* szValue)
{
 WritePrivateProfileString(szSection,  szKey, szValue, m_szFileName);
}

Main.cpp

#include "iostream"
#include 
"IniWriter.h"
#include 
"IniReader.h"
int main(int argc, char * argv[])
{
 CIniWriter iniWriter(
".\\Logger.ini");
 iniWriter.WriteString(
"Setting""Name""jianxx");   
 iniWriter.WriteInteger(
"Setting""Age"27); 
 iniWriter.WriteFloat(
"Setting""Height"1.82f); 
 iniWriter.WriteBoolean(
"Setting""Marriage"false);  
 CIniReader iniReader(
".\\Logger.ini");
 
char *szName = iniReader.ReadString("Setting""Name""");   
 
int iAge = iniReader.ReadInteger("Setting""Age"25); 
 
float fltHieght = iniReader.ReadFloat("Setting""Height"1.80f); 
 
bool bMarriage = iniReader.ReadBoolean("Setting""Marriage"true); 
 
 std::cout
<<"Name:"<<szName<<std::endl
   
<<"Age:"<<iAge<<std::endl 
   
<<"Height:"<<fltHieght<<std::endl 
   
<<"Marriage:"<<bMarriage<<std::endl; 
 delete szName;  
 
return 1;   
}