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
#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
相关文章推荐
- C++Builder学习笔记5.1(连接数据库,读写配置文件,加载DLL)
- Linux学习笔记(6)----Bash shell 登陆后配置文件的加载
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- nginx 源码学习笔记——文件读写和配置文件读取
- pom配置文件---ssm(学习笔记)
- Struts2的学习之路(四)配置文件加载顺序
- Unity Application Block 学习笔记 之 在VS2012中 配置文件 自动感应
- 4 MySQL程序概述(包含mysql配置文件配置原理)-学习笔记
- spring 配置文件详解-个人学习笔记
- springboot学习笔记2(拦截器,redis,授权登录,读取yml配置文件)
- SSH学习九 依赖注入及加载Spring配置文件的方法
- java学习笔记7 - Spring mvc 统一异常处理和静态文件的配置
- pugixml 学习笔记一 加载文件
- 电商平台学习笔记(四)——Spring配置Mybatis简化DAO层省略Mybatis核心配置文件
- 【Struts2学习笔记-4】包含其他配置文件
- 【apue学习笔记】之apue.h等头文件的配置
- ThinkPHP3.2.3学习笔记4---统计ThinkPHP3.2.3加载的文件
- nrf51822蓝牙学习笔记之GAP(通用属性配置文件)解读(二)