在程序启动前,总要配置一些参数,常用的如服务器的端口号,服务器的地址,这些启动参数就要使用本节中的知识来加载.下面给出能调试的案例,在代码中理解配置文件读取方法.
一读取ini格式的文件
1.1直接加载配置文件
1.1.2配置文件poco.ini(和程序同名)
poco.ini: name = "linyanwen" sport = 25
1.1.2例程
#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); }
本程序的输出结果:
name->"linyanwen" sport->25 appdir->/home/linuxer/Test/build-poco1-5_5-Release/ logdir->/home/linuxer/Test/build-poco1-5_5-Release/logs
1.2 另一种简单有效的方法
1.2.1配置文件
[windows] somePath = C:\test dat.dat someValue = 123 [linux] somePath = /home/linuxer someValue = 456
1.2.2例程
#include <iostream> using namespace std; #include "Poco/Util/Application.h" #include "Poco/Path.h" using namespace Poco::Util; #include "Poco/AutoPtr.h" #include "Poco/Util/IniFileConfiguration.h" using Poco::AutoPtr; using Poco::Util::IniFileConfiguration; int main(int argc, char** argv) { AutoPtr<IniFileConfiguration> pConf(new IniFileConfiguration("Test.ini")); std::string path = pConf->getString("windows.somePath"); int svalue = pConf->getInt("windows.someValue"); std::cout << path << endl; cout << svalue << endl; std::string path1 = pConf->getString("linux.somePath"); int svalue1 = pConf->getInt("linux.someValue"); std::cout << path1 << endl; cout << svalue1 << endl; return 0; }
本程序调试结果如下:
C:\test dat.dat 123 /home/linuxer 456
二 读取properties类型的文件
配置文件名:
key1 = value1 key2:123 key3.longValue = this is a Very long value path=C:\\Test.dat
例程:
#include "Poco/AutoPtr.h" #include <iostream> #include "Poco/Util/PropertyFileConfiguration.h" using Poco::AutoPtr; using Poco::Util::PropertyFileConfiguration; using namespace std; using namespace Poco::Util; int main(int argc, char** argv) { AutoPtr<PropertyFileConfiguration> pConf; pConf = new PropertyFileConfiguration("test.properties"); std::string key1 = pConf->getString("key1"); int value1 = pConf->getInt("key2"); std::string logV = pConf->getString("key3.longValue"); std::string path = pConf->getString("path"); std::cout << key1 << endl; cout << value1 << endl; cout << logV << endl; cout << path << endl; return 0; }
本程序调试结果:
value1 123 this is a Very long value C:\Test.dat
三 读取xml类型的配置文件
配置文件,名称test.xml
<config> <prop1>value1</prop1> <prop2>123</prop2> <prop3> <prop4 attr="value3" /> <prop4 attr="value4" /> </prop3> </config>
例子:
#include "Poco/AutoPtr.h" #include "Poco/Util/XMLConfiguration.h" #include <iostream> using namespace std; using Poco::AutoPtr; using Poco::Util::XMLConfiguration; int main(int argc, char** argv) { AutoPtr<XMLConfiguration> pConfig(new XMLConfiguration("test.xml")); std::string prop1 = pConfig->getString("prop1"); cout << prop1 << endl; int prop2 = pConfig->getInt("prop2"); cout << prop2 << endl; std::string prop3 = pConfig->getString("prop3"); cout << prop3 << endl; std::string prop4 = pConfig->getString("prop3.prop4"); cout << prop4 << endl; prop4 = pConfig->getString("prop3.prop4[@attr]"); cout << prop4 << endl; prop4 = pConfig->getString("prop3.prop4[1][@attr]"); cout << prop4 << endl; return 0; }
输出:
value1 123 value3 value4
本文出自 “LinuxQt济南高新区” 博客,请务必保留此出处http://qtlinux.blog.51cto.com/3052744/1701956
原文地址:http://qtlinux.blog.51cto.com/3052744/1701956