您的位置:首页 > 其它

自己写配置文件

2016-02-21 16:54 447 查看
记录一个载入配置文件的代码段,以保存以后使用

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: