main函数:
#include#include "configfile.h" using namespace std; int main() { ConfigFile cfg("test.cfg"); cout << "Dump:" << endl; cfg.dump(); cout << endl; cout << cfg.getvalue ("who") << endl; cout << cfg.getvalue ("number") << endl; cout << cfg.getvalue ("number") << endl; cout << cfg.getvalue ("notThere") << endl; cout << cfg.getvalue ("notThere", -1) << endl; cout << cfg.getvalueidx ("array", 2, -1) << endl; getchar(); return 0; }
config.h
#ifndef CONFIGFILE_H #define CONFIGFILE_H #include#include #include
config.cpp
#include "configfile.h" #includeusing namespace std; void str2lower(string &str) { for(unsigned int i=0;i filename=filename; fstream f; f.open(filename.c_str(),fstream::in); if (!f.is_open()) { return false; } string line; int lnr=-1; while (getline(f,line)) { lnr++; //Skip Comments and empty lines if (! line.length()) continue; if (line[0] == '#') continue; if (line[0] == ';') continue; int posTrenner=line.find('='); if (posTrenner==-1) posTrenner=line.find(' '); if (posTrenner==-1) { cerr << "WARNING: Statement '" << line << "' in file "<< filename << ":"< ::iterator iter= datamap.begin(); iter!=datamap.end();iter++){ cout << "key: '" << iter->first <<"' t value: '"<< iter->second <<"'"< 参考:
benreh/configfile: A very simple reader for config files written in C++ (github.com)



