您的位置:首页 > 其它

poco库学习笔记(3) 加载配置文件

2012-09-26 18:06 435 查看
一.源代码如下:

#include <Poco/Util/ServerApplication.h>

#include <Poco/Util/Application.h>

#include <string>

#include <iostream>

using Poco::Util::ServerApplication;

using Poco::Util::Application;

class AppServer:public ServerApplication{

public:

AppServer(){}

~AppServer(){}

protected:

void initialize(Application& self){

ServerApplication::loadConfiguration();//加载配置文件

ServerApplication::initialize(self);

}

void uninitialize(){

ServerApplication::uninitialize();

}

int main(const std::vector<std::string>& args){

std::string name = config().getString("name");//读取配置文件

int sport = config().getInt("sport");//读取配置文件

std::string appdir = config().getString("application.dir","./");

std::string logdir = config().getString("logs.directory",appdir+"logs");

std::cout<<"name->"<<name<<std::endl;

std::cout<<"sport->"<<sport<<std::endl;

std::cout<<"appdir->"<<appdir<<std::endl;

std::cout<<"logdir->"<<logdir<<std::endl;

return Application::EXIT_OK;

}

};

int main(int argc,char** argv){

AppServer as;

return as.run(argc,argv);

}

二.在程序的启动目录下写一个同名字的配置文件,文件的后缀名可以为ini,xml,properties,这里选用ini做为后缀名,配置文件main.ini内容如下:

main.ini:

name = "linyanwen"

sport = "football"

三.编译

参考前面写的makefile文件,只需执行命令make即可.

四.运行

执行命令./main即可,顺利的话,会输出配置文件中相应的内容,也可以给程序传递命令行参数,这些命令行参数会传递给AppServer类中的main函数的参数const std::vector<std::string>& args中

PS:初写文章,文笔生涩之处,各位请见谅,若有疑问或者交流的,可加本人YY号:301558660
转载请注明出处:山水间博客:/article/2317618.html

本文参考:http://blog.csdn.net/poechant/article/details/7484781
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: