头文件
#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");