分享

VC 读写ini文件的封装类~ (转)

 汇英四方 2014-02-18

记得很久以前用过,可今天用起来还是有的不太顺.索性做成一个类,方便以后使用.

 .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);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多