VC++中,配置文件的读写操作

来源:互联网 发布:linux usleep range 编辑:程序博客网 时间:2024/06/04 18:33

配置文件写操作:

(1)获得应用程序路径的 函数:   

CString CAppFullPath::AppPath()
{
 CString strModulePath;
 DWORD dwResult = ::GetModuleFileName(NULL,strModulePath.GetBuffer(MAX_PATH),MAX_PATH);
 ASSERT(dwResult);
 strModulePath.ReleaseBuffer();
 strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('//')) + 1);
 return strModulePath;
}

(2)获得配置文件路径:strtranspath=(应用程序路径) strModulePath+(文件名)*.ini

(3)写 .ini 配置文件:       WritePrivateProfileString

BOOL  writetransport=WritePrivateProfileString("Transrefer.ini","IPPORT",t_serverport,strtranspath);  
                                                                     "配置文件名" ,"字段"    ,   值         ,配置文件路径

  ASSERT(writetransport);   //验证是否为真,假时,程序卡在这,报错。 

读取配置文件:

(1)CFileFind finder;//定义一标准类的对象
 BOOL finded;
 finded=finder.FindFile(transpath);//是否查找到文件 (内为文件路径)
 if(finded)
 {
   GetPrivateProfileString("Transrefer.ini","IPADDRESS","127.0.0.1",serverip.GetBuffer(20),20,transpath);
                           文件中第一行的内容 ,字段 ,  默认值, 存储地,20为字段长度, 文件路径
   serverip.ReleaseBuffer();
  }