首页 > 代码库 > 自己写配置文件

自己写配置文件

记录一个加载配置文件的代码段,以保存以后使用

bool Consumer::loadConfigFile()

{
if( ConfigMap.size() != 0 )
ConfigMap.clear();
char szFilePath[256]={0};  
GetModuleFileNameA(NULL, szFilePath, 255);  
(strrchr(szFilePath, ‘\\‘))[0] = 0; // 删除文件名,只获得路径字串 
std::string path = szFilePath;
path+= "\\config.bat";




FILE* fp = fopen( path.c_str(), "r");
if(fp == NULL)
{
char buf[128];
sprintf_s(buf,"%s open failed ",path);
ExtLogger.Out( buf );
return false;
}
char buff[50];
while(fgets(buff, 50, fp))
{
for(int i=0; i< strlen(buff); i++)
{
if(buff[i] == ‘\n‘)
buff[i] = 0;
}
std::string strSymbol = buff;
int pos = strSymbol.find(‘=‘,0);
ConfigMap[strSymbol.substr(0,pos)] = strSymbol.substr(pos+1, strSymbol.length()-pos-1);
}
if( ConfigMap.size() == 0 )
{
ExtLogger.Out( "ConfigFile %s can not read any data.", path.c_str() ); 
return false;
}
ExtLogger.Out( "configuration file is Loaded successfully!" );
fclose(fp);
return true;
}