钱报速递有限公司:读取配置文件函数
来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 13:36:03
1、readcfg.hh
#include#include #include #include using namespace std;
class readCfg{ private: ifstream Ffp; string Ffilename; void reReadCfg(); void closeFile(); public: readCfg(string filename); string readValue(string session,string key); ~readCfg(); string getFilename(); };
2、readcfg.cc
#include#include#includeusing namespace std;#include "readcfg.hh"
readCfg::readCfg(string filename){ Ffilename = filename; cout<< "Filename is:" << Ffilename << endl;}
readCfg::~readCfg(){ closeFile();}
void readCfg::reReadCfg(){ closeFile(); try { Ffp.open(Ffilename.c_str(),ios::in); }catch(exception& ex) { cout << "Open File error!" << endl; cout << "Exception caught is :" << ex.what() << endl ; } }
void readCfg::closeFile(){ try { Ffp.close(); if(!Ffp.good()) { Ffp.clear(); }
}catch(exception& ex) { cout << "Close File error!" << endl; cout << "Exception caught is: " << ex.what() << endl; } }string readCfg::readValue(string session,string key){ reReadCfg();
string line, buf; bool sessionFlag = false; bool valueFlag = false;
while(!Ffp.eof()) { Ffp >> line; if(line[0] == '#') continue; if(line[0] != '[' && !sessionFlag) continue; else if(line[0] == '[' && !sessionFlag) { string tmp("["+session+"]"); if(line.compare(tmp)==0) { sessionFlag = true; }else { continue; }
}else if(sessionFlag) { string tmpkey(key+"="); if(line.compare(0,tmpkey.size(),tmpkey)==0) { valueFlag = true; buf = line.substr(tmpkey.size());// cout << "value=" << line.substr(tmpkey.size()) << endl;// cout << "buf="<< buf << endl; } if(line[0] == '[') { break; } } }
if(!valueFlag) { cout << "Not found this value." << endl; } else return buf;
}
string readCfg::getFilename(){ return Ffilename;}
3、main.cc
#include#include#includeusing namespace std;#include "readcfg.hh"
int main(){ readCfg fin("test.ini"); cout << fin.getFilename() << endl; cout << fin.readValue("cca","name") < exit(0);
}
4、test.ini
[cca]name=ccaage=1
[pro]name=proage=2
[123]name=123age=123
#include
class readCfg{ private: ifstream Ffp; string Ffilename; void reReadCfg(); void closeFile(); public: readCfg(string filename); string readValue(string session,string key); ~readCfg(); string getFilename(); };
2、readcfg.cc
#include
readCfg::readCfg(string filename){ Ffilename = filename; cout<< "Filename is:" << Ffilename << endl;}
readCfg::~readCfg(){ closeFile();}
void readCfg::reReadCfg(){ closeFile(); try { Ffp.open(Ffilename.c_str(),ios::in); }catch(exception& ex) { cout << "Open File error!" << endl; cout << "Exception caught is :" << ex.what() << endl ; } }
void readCfg::closeFile(){ try { Ffp.close(); if(!Ffp.good()) { Ffp.clear(); }
}catch(exception& ex) { cout << "Close File error!" << endl; cout << "Exception caught is: " << ex.what() << endl; } }string readCfg::readValue(string session,string key){ reReadCfg();
string line, buf; bool sessionFlag = false; bool valueFlag = false;
while(!Ffp.eof()) { Ffp >> line; if(line[0] == '#') continue; if(line[0] != '[' && !sessionFlag) continue; else if(line[0] == '[' && !sessionFlag) { string tmp("["+session+"]"); if(line.compare(tmp)==0) { sessionFlag = true; }else { continue; }
}else if(sessionFlag) { string tmpkey(key+"="); if(line.compare(0,tmpkey.size(),tmpkey)==0) { valueFlag = true; buf = line.substr(tmpkey.size());// cout << "value=" << line.substr(tmpkey.size()) << endl;// cout << "buf="<< buf << endl; } if(line[0] == '[') { break; } } }
if(!valueFlag) { cout << "Not found this value." << endl; } else return buf;
}
string readCfg::getFilename(){ return Ffilename;}
3、main.cc
#include
int main(){ readCfg fin("test.ini"); cout << fin.getFilename() << endl; cout << fin.readValue("cca","name") <
}
4、test.ini
[cca]name=ccaage=1
[pro]name=proage=2
[123]name=123age=123
读取配置文件函数
创建控制台应用程序读取配置文件的内容
在C#中如何读取配置文件 - CookBlack - 博客园
gvim配置文件
Hibernate配置文件
ftp 配置文件
利用ASP读取RSS
读取文件中的坐标
读取txt文件
Sdcard的读取问题
cin读取失败怎么办?
[JAVA]读取BMP图
读取指定行数据
“word无法读取1
内存读取错误是怎么回事
vb读取excel内容
mysql配置文件解析
Struts配置文件详解
理解 Linux 配置文件 11
Log4cxx配置文件介绍
理解Linux配置文件
Linux 配置文件介绍
nginx配置文件祥解
rtorrent配置文件全解释