最近项目用的比较多的写入写出参数配置文件函数,个人觉得挺好用的,所以记录下来方便学习。
GetPrivateProfileString
和WritePrivateProfileString
都属于INI文件编程中的WINAPI函数,直接介绍。
写入INI文件
WINAPI函数原型为:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName,//INI文件中的一个字段名.
LPCTSTR lpKeyName,//lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString,//键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName//完整的INI文件名.
);
举例使用
将一名学生的姓名写入 c:\stud\student.ini
文件中.
CString strName="李四";
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");
此时c:\stud\student.ini
文件中的内容如下:
[StudentInfo]
Name=李四
读取INI文件
WINAPI函数原型为:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName,//INI文件中的一个字段名.
LPCTSTR lpKeyName,//lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpDefault,//如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
LPTSTR lpReturnedString,//接收INI文件中的值的CString对象,即目的缓存器.
DWORD nSize,//目的缓存器的大小.
LPCTSTR lpFileName//是完整的INI文件名.
);
举例使用
将上面写入的信息读出到程序中:
CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");
//执行后 strStudName 的值为:"李四",若前两个参数有误,其值为:"默认姓名".
读入整型值要用另一个WINAPI函数:
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
int nStudAge;
nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");
扩展
了解INI文件的结构:
;注释
[小节名]
关键字=值
…
INI文件允许有多个小节,每个小节又允许有多个关键字, "=“后面是该关键字的值。
值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,
布尔真值用1表示,布尔假值用0表示。
注释以分号”;"开头。
简化一
void WINAPI ReadIni (CString strPath,CString strSec,CString strKey,CString &strContent)
{
CString strTemp;
char cTemp[256];
GetPrivateProfileString(strSec,strKey,"",cTemp,255,strPath);
strContent=cTemp;
}
void WINAPI WriteIni(CString strPath,CString strSec,CString strKey,CString strValue)
{
::WritePrivateProfileString(strSec,strKey,strValue,strPath);
}
//使用
CString strImportIni;
CString strLen;
int nExIndexSp1;
strImportIni = g_ExePath+"\\ImpotFileParam.ini";
ReadIni(strImportIni,"LineKnife","LK",strLen);
nExIndexSp1 = atoi(strLen);
WriteIni(strImportIni, "OtherParam", "ToolDefaults",str);
简化二
#define PCHLEN 512 //一般字符串长度
CString m_strLanguagePath; // 配置路径
CString SysLanguageInfo::GetCs(CString strSection, CString strKey, CString strDefault)
{
CString strValue = "";
char buf[PCHLEN] = "";
int nRet = GetPrivateProfileString(strSection, strKey, "", buf, PCHLEN, m_strLanguagePath);
if (nRet <= 0)
{
if ( !strDefault.IsEmpty() )
{
WritePrivateProfileString(strSection, strKey, strDefault, m_strLanguagePath);
strValue = strDefault;
}
else
{
WritePrivateProfileString(strSection, strKey, strKey, m_strLanguagePath);
strValue = strKey;
}
}
else
{
strValue = buf;
}
return strValue;
}
//使用
iKey = MULTILANGUAGESTART + 2019;
g_LanguageKey.Format("%d", iKey);
g_strTranslate = "The cutting bed is running, please wait to finish!";
g_strTranslate = g_SysLanguageInfo.GetCs("Prompted", g_LanguageKey, g_strTranslate);
AfxMessageBox(g_strTranslate);
简化三
CString CMainFrame::GetSysDateInfo(CString strMainKey, CString strKey, CString strDefault, CString strIniPath, BOOL bReadOrWrite)
{
CString strValue;
int iValue = 0;
char buf[PCHLEN] = "";
memset(buf, 0, sizeof(buf));
if (bReadOrWrite)
{
WritePrivateProfileString(strMainKey, strKey, strDefault, strIniPath);
strValue = strDefault;
}
else
{
int nRet = 0;
nRet = GetPrivateProfileString(strMainKey, strKey, "", buf, PCHLEN, strIniPath);
if (nRet <= 0)
{
WritePrivateProfileString(strMainKey, strKey, strDefault, strIniPath);
strValue = strDefault;
}
else
{
strValue = buf;
}
}
return strValue;
}
//使用
CString strFeedDelay;
strFeedDelay = pMain->GetSysDateInfo("Feed", "Delay", strDefault, g_ExePath + _T("Config\\ImpotFileParam.ini"));