记得很久以前用过,可今天用起来还是有的不太顺.索性做成一个类,方便以后使用.
.h文件
class CIniFile { public: CIniFile(); virtual ~CIniFile(); // //获得section值 int GetSectionValue(CString &strName); CString GetSectionValueS(CString &strName); BOOL SetSectionValue(LPCTSTR strValue,CString &strName); private: CString sFilePath; CString strSection; CString strIniFileName ; };
.cpp文件
CIniFile::CIniFile() { int nPos; strSection = "SET DEFAULT VALUE"; GetModuleFileName(NULL,sFilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH); sFilePath.ReleaseBuffer ();
nPos=sFilePath.ReverseFind(‘\\’); sFilePath=sFilePath.Left (nPos); strIniFileName = sFilePath + "http://cache./file://config.ini/"; }
CIniFile::~CIniFile() {
} int CIniFile::GetSectionValue(CString &strName) { int nCount= GetPrivateProfileInt(strSection.GetBuffer(0),strName.GetBuffer(0),0,strIniFileName); return nCount; } BOOL CIniFile::SetSectionValue(LPCTSTR strValue,CString &strName) { BOOL bRect = WritePrivateProfileString(strSection.GetBuffer(0),strName.GetBuffer(0),strValue, strIniFileName); return bRect; } CString CIniFile::GetSectionValueS(CString &strName) { CString strSect; GetPrivateProfileString(strSection.GetBuffer(0),strName.GetBuffer(0),"默认",strSect.GetBuffer(MAX_PATH),MAX_PATH,strIniFileName); return strSect; }
测试代码
CIniFile file; CString str="Name"; CString str2= "AGE"; file.SetSectionValue("Li",str); file.SetSectionValue("19",str2);
CString str3 = file.GetSectionValueS(str); int i = file.GetSectionValue(str2);
|