c/c++常用代码 -- ini文件操作
#pragma once #include <string> #include <sstream> typedef std::basic_string<TCHAR> tstring; class CIniCfg { public: CIniCfg() { TCHAR szTemp[MAX_PATH]; GetModuleFileName(NULL, szTemp, sizeof(szTemp)); LPTSTR p = _tcsrchr(szTemp, _T(‘.‘)); if (p != NULL) _tcscpy(p, _T(".ini")); else _tcscat(szTemp, _T(".ini")); m_strFilePath = szTemp; } void SetName(LPCTSTR szName) { TCHAR szTemp[MAX_PATH]; GetModuleFileName(NULL, szTemp, sizeof(szTemp)); LPTSTR p = _tcsrchr(szTemp, _T(‘\\‘)); if (p != NULL) { _tcscpy(++p, szName); } m_strFilePath = szTemp; } void SetPath(LPCTSTR szPath) { m_strFilePath = szPath; } BOOL SetInt(LPCTSTR lpAppName, // pointer to section name LPCTSTR lpKeyName, // pointer to key name int nValue) { std::basic_stringstream<TCHAR> ss; ss << nValue; return WritePrivateProfileString( lpAppName, lpKeyName, ss.str().c_str(), //strValue, m_strFilePath.c_str()); } BOOL SetString(LPCTSTR lpAppName, // pointer to section name LPCTSTR lpKeyName, // pointer to key name LPCTSTR lpString) // pointer to string to add { return WritePrivateProfileString( lpAppName, lpKeyName, lpString, m_strFilePath.c_str()); } int GetInt(LPCTSTR lpAppName, // address of section name LPCTSTR lpKeyName, // address of key name int nDefault) // return value if key name is not found { return GetPrivateProfileInt( lpAppName, lpKeyName, nDefault, m_strFilePath.c_str()); } tstring GetString(LPCTSTR lpAppName, // points to section name LPCTSTR lpKeyName, // points to key name LPCTSTR lpDefault) // points to default string { TCHAR szRet[MAX_PATH] = {0}; DWORD dwSize = MAX_PATH; GetPrivateProfileString( lpAppName, // points to section name lpKeyName, // points to key name lpDefault, // points to default string szRet, // points to destination buffer dwSize, // size of destination buffer m_strFilePath.c_str()); return szRet; } protected: tstring m_strFilePath; };
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。