Win32 ini配置文件的读写
2013-03-28 16:59
239 查看
ini配置文件的固定格式都是
[BACK]
BACKSET=3//这种格式方便存储数据,成为一种通用的数据,
这里总结下对ini文件的读写
往[BACK]中的BACKSET键中写值,以int为例
test_ini.WriteInt(L"BACK",L"BACKSET",countbk),test _ini是一个ini文件的读写类,下面是Writeint函数的实现
BOOL CIniFile::WriteInt(LPCTSTR lpSection, LPCTSTR lpKey, int nValue) const
{
TCHAR szValue[DEF_PROFILE_NUM_LEN + 1] = _T("");
_stprintf(szValue, _T("%d"), nValue);
return WriteString(lpSection, lpKey, szValue);
}
BOOL CIniFile::WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue) const
{
if (lpSection == NULL || lpKey == NULL)
return FALSE;
return CeWritePrivateProfileString(lpSection, lpKey, lpValue == NULL ? _T("") : lpValue, m_pszPathName);
}
而最关键的就是CeWritePrivateProfileString(lpSection, lpKey, lpValue == NULL ? _T("") : lpValue, m_pszPathName);函数的实现,说个大概思想吧
打开你所需要的写入的文件,寻找‘[’字符和']'字符。存储中间的字符串和”BACK“比较,找到相等的再找“BACKSET”键值,修改等号后面的数据,具体代码网上有
封装的很好了。
ini文件读取
test_ini.GetInt(L"BACK",L"BACKSET", 0);
int CIniFile::GetInt(LPCTSTR lpSection, LPCTSTR lpKey, int nDefault) const
{
TCHAR sz[DEF_PROFILE_NUM_LEN + 1] = _T("");
GetString(lpSection, lpKey, sz, DEF_PROFILE_NUM_LEN);
return *sz == _T('\0') ? nDefault : int(_tcstoul(sz, NULL, 10));
}
DWORD CIniFile::GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDefault) const
{
if (lpBuffer != NULL)
*lpBuffer = _T('\0');
LPTSTR psz = __GetStringDynamic(lpSection, lpKey, lpDefault);
DWORD dwLen = _tcslen(psz);
if (lpBuffer != NULL)
{
_tcsncpy(lpBuffer, psz, dwBufSize);
dwLen = min(dwLen, dwBufSize);
}
delete [] psz;
return dwLen;
}
LPTSTR CIniFile::__GetStringDynamic(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpDefault) const
{
TCHAR* psz = NULL;
if (lpSection == NULL || lpKey == NULL)
{
// Invalid section or key name, just return the default string
if (lpDefault == NULL)
{
// Empty string
psz = new TCHAR[1];
*psz = _T('\0');
}
else
{
psz = new TCHAR[_tcslen(lpDefault) + 1];
_tcscpy(psz, lpDefault);
}
return psz;
}
// Keep enlarging the buffer size until being certain on that the string we
// retrieved was original(not truncated).
DWORD dwLen = DEF_PROFILE_THRESHOLD;
psz = new TCHAR[dwLen + 1];
DWORD dwCopied = CeGetPrivateProfileString(lpSection, lpKey, lpDefault == NULL ? _T("") : lpDefault, psz, dwLen, m_pszPathName);
while (dwCopied + 1 >= dwLen)
{
dwLen += DEF_PROFILE_THRESHOLD;
delete [] psz;
psz = new TCHAR[dwLen + 1];
dwCopied = CeGetPrivateProfileString(lpSection, lpKey, lpDefault == NULL ? _T("") : lpDefault, psz, dwLen, m_pszPathName);
}
return psz; // !!! Requires the caller to free this memory !!!
}
添加了IniFile头文件后,调用过程就比较简单了
CStdString 也可以写了
FILE *stream;
stream = _wfopen(GPSPATH, _T("w+"));
if(stream == NULL)
return;
else
{
CIniFile IniFile;
IniFile.SetPathName(GPSPATH);
printf("MoveFolder::tmpPath = %ls\n",tmpPath);
IniFile.WriteString(L"MAP", L"MapPath", (LPCTSTR)tmpPath);
}
读取CStdString
BOOL bRet = FALSE;
FILE *stream;
stream = _wfopen(GPSPATH, _T("r"));
if(stream == NULL)
bRet = FALSE;
else
{
CIniFile IniFile;
IniFile.SetPathName(GPSPATH);
TCHAR str[MAX_PATH+1] = _T("");
IniFile.GetString(L"MAP",L"MapPath", str, MAX_PATH);
}
注意定义的时候一定要将MAX_PATH+1.
[BACK]
BACKSET=3//这种格式方便存储数据,成为一种通用的数据,
这里总结下对ini文件的读写
往[BACK]中的BACKSET键中写值,以int为例
test_ini.WriteInt(L"BACK",L"BACKSET",countbk),test _ini是一个ini文件的读写类,下面是Writeint函数的实现
BOOL CIniFile::WriteInt(LPCTSTR lpSection, LPCTSTR lpKey, int nValue) const
{
TCHAR szValue[DEF_PROFILE_NUM_LEN + 1] = _T("");
_stprintf(szValue, _T("%d"), nValue);
return WriteString(lpSection, lpKey, szValue);
}
BOOL CIniFile::WriteString(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue) const
{
if (lpSection == NULL || lpKey == NULL)
return FALSE;
return CeWritePrivateProfileString(lpSection, lpKey, lpValue == NULL ? _T("") : lpValue, m_pszPathName);
}
而最关键的就是CeWritePrivateProfileString(lpSection, lpKey, lpValue == NULL ? _T("") : lpValue, m_pszPathName);函数的实现,说个大概思想吧
打开你所需要的写入的文件,寻找‘[’字符和']'字符。存储中间的字符串和”BACK“比较,找到相等的再找“BACKSET”键值,修改等号后面的数据,具体代码网上有
封装的很好了。
ini文件读取
test_ini.GetInt(L"BACK",L"BACKSET", 0);
int CIniFile::GetInt(LPCTSTR lpSection, LPCTSTR lpKey, int nDefault) const
{
TCHAR sz[DEF_PROFILE_NUM_LEN + 1] = _T("");
GetString(lpSection, lpKey, sz, DEF_PROFILE_NUM_LEN);
return *sz == _T('\0') ? nDefault : int(_tcstoul(sz, NULL, 10));
}
DWORD CIniFile::GetString(LPCTSTR lpSection, LPCTSTR lpKey, LPTSTR lpBuffer, DWORD dwBufSize, LPCTSTR lpDefault) const
{
if (lpBuffer != NULL)
*lpBuffer = _T('\0');
LPTSTR psz = __GetStringDynamic(lpSection, lpKey, lpDefault);
DWORD dwLen = _tcslen(psz);
if (lpBuffer != NULL)
{
_tcsncpy(lpBuffer, psz, dwBufSize);
dwLen = min(dwLen, dwBufSize);
}
delete [] psz;
return dwLen;
}
LPTSTR CIniFile::__GetStringDynamic(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpDefault) const
{
TCHAR* psz = NULL;
if (lpSection == NULL || lpKey == NULL)
{
// Invalid section or key name, just return the default string
if (lpDefault == NULL)
{
// Empty string
psz = new TCHAR[1];
*psz = _T('\0');
}
else
{
psz = new TCHAR[_tcslen(lpDefault) + 1];
_tcscpy(psz, lpDefault);
}
return psz;
}
// Keep enlarging the buffer size until being certain on that the string we
// retrieved was original(not truncated).
DWORD dwLen = DEF_PROFILE_THRESHOLD;
psz = new TCHAR[dwLen + 1];
DWORD dwCopied = CeGetPrivateProfileString(lpSection, lpKey, lpDefault == NULL ? _T("") : lpDefault, psz, dwLen, m_pszPathName);
while (dwCopied + 1 >= dwLen)
{
dwLen += DEF_PROFILE_THRESHOLD;
delete [] psz;
psz = new TCHAR[dwLen + 1];
dwCopied = CeGetPrivateProfileString(lpSection, lpKey, lpDefault == NULL ? _T("") : lpDefault, psz, dwLen, m_pszPathName);
}
return psz; // !!! Requires the caller to free this memory !!!
}
添加了IniFile头文件后,调用过程就比较简单了
CStdString 也可以写了
FILE *stream;
stream = _wfopen(GPSPATH, _T("w+"));
if(stream == NULL)
return;
else
{
CIniFile IniFile;
IniFile.SetPathName(GPSPATH);
printf("MoveFolder::tmpPath = %ls\n",tmpPath);
IniFile.WriteString(L"MAP", L"MapPath", (LPCTSTR)tmpPath);
}
读取CStdString
BOOL bRet = FALSE;
FILE *stream;
stream = _wfopen(GPSPATH, _T("r"));
if(stream == NULL)
bRet = FALSE;
else
{
CIniFile IniFile;
IniFile.SetPathName(GPSPATH);
TCHAR str[MAX_PATH+1] = _T("");
IniFile.GetString(L"MAP",L"MapPath", str, MAX_PATH);
}
注意定义的时候一定要将MAX_PATH+1.
相关文章推荐
- 【转载】C++针对ini配置文件读写大全
- 用java读写ini配置文件的原因以及实现 示例
- 一个对ini配置文件读写的类
- C++读写ini配置文件
- winform读写ini配置文件的类及相关调用
- WritePrivateProfileString和GetPrivateProfileString读写ini配置文件
- WritePrivateProfileString等读写.ini配置文件
- C#中读写INI配置文件
- Python编程:ini配置文件读写
- INI配置文件读写工具类
- C++针对ini配置文件读写大全
- 在C#中读写INI配置文件(转)
- QT读写ini配置文件
- Python实现读写INI配置文件的方法示例
- C++针对ini配置文件读写大全
- Python读写ini配置文件
- ini配置文件读写
- Qt读写INI配置文件
- win32 读写 ini 文件
- C语言ini格式配置文件的读写