您的位置:首页 > 其它

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: