简单写ini配置文件的方式(备份)

前言

有时候需要将一些参数,或者算法运算结果保存在ini文件中,那么就需要有保存参数的功能了。

一、函数解释

在VC中需要#include<windows.h>头文件,其函数分别为:

写入.ini文件:

bool WritePrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpString,LPCTSTR lpFileName);

读取.ini文件:

DWORD GetPrivateProfileString(LPCTSTR lpAppName,LPCTSTR lpKeyName,LPCTSTR lpDefaut,LPSTR lpReturnedString,DWORD nSize,LPCTSTR lpFileName);

读取整形值:

UINT GetPrivateProfileInt(LPCTSTR lpAppName,LPCTSTR lpKeyName,INT nDefault,LPCTSTR lpFileName);


二、参数的解释:
LPCTSTR lpAppName ------- INI文件中的一个字段名
LPCTSTR lpKeyName -------- lpAppName 下的一个键名,也就是里面具体的变量名
LPCTSTR lpString ---------是键值,也就是变量的值, 必须为LPCTSTR或CString类型
LPCTSTR lpFileName --------完整的INI文件路径名
LPCTSTR lpDefaut ----------如果没有其前两个参数值,则将此值赋给变量
LPSTR lpReturnedString --------接收INI文件中的值的CString对象,即接收缓冲区
DWORD nSize ------接收缓冲区的大小

三、例子

CString StrName,Strtemp;

int nAge;

StrName = "jacky";

nAge = 13;

WritePrivateProfileString("Student","Name",StrName,"c:\\setting.ini");

结果:(INI文件中显示如下:)

[Student]

Name=jacky

读取:

CString SName;

GetPrivateProfileString("Student","Name","DefaultName",SName.GetBuffer(MAX_LENGTH),MAX_LENGTH,"c:\\setting.ini");

结果:SName = "jacky";这里需要注意点就是用完GetBuffer函数后一定要释放(用SName.ReleaseBuffer()函数),不然后面再用到SName的其他子函数就会失灵。

读整数比较简单,如下

int Result = GetPrivateProfileInt("Student","nAge",0,"c:\\setting.ini")返回值即为所读取的结果!

在GetPrivateProfileString最后一个参数是配置文件路径的参数,此路径只能是绝对路径,不能是相对路径,但现在我需要是我的exe文件能和我的配置文件在一起。因此我使用了GetCurrentDirectory函数。

原代码如下:

CString server_ip;
CString des="";
::GetCurrentDirectory(MAX_PATHLENGTH,des.GetBuffer(MAX_PATHLENGTH));
des.ReleaseBuffer();
des+="\\config.ini";
GetPrivateProfileString("PhoneDemo","Server_IP","",server_ip.GetBufferSetLength(15),15,des);
server_ip.ReleaseBuffer();

注意:在这里使用CString变量时,在使用完GetBuffer后,紧接着一定要使用ReleaseBuffer()函数,才可以进行其他的诸如字符串+操作

 

 

char m_szCurrPath[255];   
GetModuleFileName(::GetModuleHandle(NULL),m_szCurrPath,255); 
(_tcsrchr(m_szCurrPath, _T('\\')))[1] = 0; //删除文件名,只获得路径
CString IniPath;
CString IPAddress;
IniPath.Format("%sCTC.ini",m_szCurrPath);
//读取ini文件
GetPrivateProfileString("DataBase","IP","IP",IPAddress.GetBuffer(255),255,IniPath);

四、简单的写ini文件的另一种方式

avoid writeConfigFile(MyParam param)
{

	string filePath = ".\\Config.ini";
	String headLineText = "[大标题]\n";

	std::ofstream oFile;
	oFile.open(filePath, std::ios::out | std::ios::trunc);if (oFile)
	{
		oFile.write(headLineText.c_str(), headLineText.length());
		headLineText = "name=" + to_string(param.name) + "\n";
		oFile.write(headLineText.c_str(), headLineText.length());
		headLineText = "id=" + to_string(param.id) + "\n";
		oFile.write(headLineText.c_str(), headLineText.length());
		headLineText = "score=" + to_string(param.score) + "\n";
		oFile.write(headLineText.c_str(), headLineText.length()); 
	}
	oFile.flush();
	oFile.close();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玛莎拉丶帝

对您有帮助的话,请我喝杯饮料!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值