.h文件
#pragma once
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "atlstr.h"
class CIniRw
{
public:
CIniRw(void);
~CIniRw(void);
public:
bool GetFileStatus(CString strFileName);
void SetFileName(CString strFileName);
public:
void WriteInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nValue);
void WriteFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbValue);
void WriteBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bValue);
void WriteString(LPCTSTR lpSection, LPCTSTR lpKey, CString szValue);
int ReadInteger(LPCTSTR lpSection, LPCTSTR lpKey, int iDefaultValue);
double ReadFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbDefaultValue);
bool ReadBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bolDefaultValue);
CString ReadString(LPCTSTR lpSection, LPCTSTR lpKey, CString szDefaultValue);
public:
CString m_strFileName ;
};
.cpp文件
#include "StdAfx.h"
#include "IniRw.h"
CIniRw::CIniRw(void)
{
}
CIniRw::~CIniRw(void)
{
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:获取文件状态
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
bool CIniRw::GetFileStatus(CString strFileName)
{
CString strStatus;
TCHAR sPath[MAX_PATH];
CString strPath, strConfigPath;
GetModuleFileName(NULL, sPath, MAX_PATH);
strPath = sPath;
strPath = strPath.Left(strPath.ReverseFind( _T('\\')));
strConfigPath = strPath + _T("\\") + strFileName;
m_strFileName = strConfigPath;
strStatus = ReadString(_T("FileStatus"), _T("status"), _T("noFile"));
if (strStatus == _T("fileExist"))
{
return true;
}
else
{
//WriteString(_T("FileStatus"), _T("status"), _T("fileExist"));
return false;
}
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:设置文件名
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::SetFileName(CString strFileName)
{
TCHAR sPath[MAX_PATH];
CString strPath, strConfigPath;
GetModuleFileName(NULL, sPath, MAX_PATH);
strPath = sPath;
strPath = strPath.Left(strPath.ReverseFind( _T('\\')));
strConfigPath = strPath + _T("\\") + strFileName;
m_strFileName = strConfigPath;
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:写整型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nValue)
{
CString strValue;
strValue.Format(_T("%d"), nValue);
WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName);
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:写浮点型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbValue)
{
CString strValue;
strValue.Format(_T("%.06f"), dbValue);
WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName);
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:写布尔型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bValue)
{
CString strValue;
if (bValue)
strValue.Format(_T("true"));
else
strValue.Format(_T("false"));
WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName);
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:写字符串
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteString(LPCTSTR lpSection, LPCTSTR lpKey, CString strValue)
{
WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName);
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:读整型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
int CIniRw::ReadInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nDefaultValue)
{
int nResult;
nResult = GetPrivateProfileInt(lpSection, lpKey, nDefaultValue, m_strFileName);
return nResult;
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:写浮点型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
double CIniRw::ReadFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbDefaultValue)
{
TCHAR szResult[255];
TCHAR szDefault[255];
double dbResult;
GetPrivateProfileString(lpSection, lpKey, szDefault, szResult, 255, m_strFileName);
dbResult = atof(szResult);
return dbResult;
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:读布尔型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
bool CIniRw::ReadBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bDefaultValue)
{
TCHAR szResult[255];
TCHAR szDefault[255];
CString strValue ;
bool bResult = false;
GetPrivateProfileString(lpSection, lpKey, szDefault, szResult, 255, m_strFileName);
strValue = szResult;
if (strValue == _T("false"))
bResult = false;
else if (strValue == _T("true"))
bResult = true;
return bResult;
}
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
//描述:读字符串
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
CString CIniRw::ReadString(LPCTSTR lpSection, LPCTSTR lpKey, CString strDefultValue)
{
TCHAR szResult[255];
CString strResult;
memset(szResult, 0x00, 255);
GetPrivateProfileString(lpSection, lpKey, strDefultValue, szResult, 255, m_strFileName);
strResult.Format(szResult);
return strResult;
}
源代码下载:https://download.csdn.net/download/sunriver2000/10321121