自己写配置文件
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;
}
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;
}
相关文章推荐
- Binary Tree Right Side View
- 将一句话中单词进行倒置,标点符号不倒置
- Swift实现IOS界面的跳转
- 利用AVPlayer播放iOS沙盒中的视频
- 被嘲笑没有智商的题
- 1. redis简介
- HNIO 2013 bzoj 3139 比赛
- 官方开发文档翻译-Service
- [BZOJ2002] [Hnoi2010]Bounce 弹飞绵羊
- 乐视生态世界发布会官方图文直播(2016年01月12日 15:00)
- Java读取properties文件
- C#设计模式学习笔记-单例模式
- 算法训练 猴子分苹果
- 248,AFNetworking 3的使用(一)
- Dungeon Master(逃脱大师)-BFS
- 在已有 Xcode 项目中 加入Cordova框架
- 数据库操作
- ubuntu 添加开机启动项
- CentOS7下Firewall防火墙配置用法详解
- Window 系统 “位图”