c++读取配置文件ini类

头文件

#pragma once
class CReadIniClass
{
public:
	CReadIniClass(void);
	virtual ~CReadIniClass(void);

	BOOL GetFilePath(CString strPath);
	BOOL WriteIniFile(CString strAppName, CString strKeyName, CString strValue);
	BOOL ReadIniFile(CString strAppName, CString strKeyName, CString &strValue);
private:
	CString m_strFilePath;
};

cpp实现

#include "StdAfx.h"
#include "ReadIniClass.h"


CReadIniClass::CReadIniClass(void)
{
}


CReadIniClass::~CReadIniClass(void)
{
}

BOOL CReadIniClass::GetFilePath(CString strPath)
{
	if(strPath.IsEmpty())
	{
		return FALSE;
		
	}
	m_strFilePath = strPath;
	return TRUE;
}

BOOL CReadIniClass::WriteIniFile(CString strAppName, CString strKeyName, CString strValue)
{
	if (strAppName.IsEmpty() || strKeyName.IsEmpty() || strValue.IsEmpty())
	{
		return FALSE;
	}

	::WritePrivateProfileString(strAppName, strKeyName, strValue, m_strFilePath);
	return TRUE;
}

BOOL CReadIniClass::ReadIniFile(CString strAppName, CString strKeyName, CString &strValue)
{
	if (strAppName.IsEmpty() || strKeyName.IsEmpty())
	{
		return FALSE;
	}
	CString str;
	::GetPrivateProfileString(strAppName, strKeyName, NULL, str.GetBuffer(256), 256, m_strFilePath);
	str.ReleaseBuffer();
	strValue = str;

	if (strValue.IsEmpty())
	{
		return FALSE;
	}
	return TRUE;
}

char buf[129];
CString port;
	GetPrivateProfileString("设置","端口号","default",buf,128,"./stage.ini");
	port.Format("%s",buf);
	WritePrivateProfileString("设置","端口号",Port,"./stage.ini");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值