本文只看粗体即可,太多catch语句。两个例子均来自libconfig包的example文件夹下面,.

例子一:

#include <iostream>

#include <iomanip>

#include <cstdlib>

#include <libconfig.h++>

using namespace std;

using namespace libconfig;

// This example reads the configuration file 'example.cfg' and display some of its contents.



int main(int argc, char **argv)

{

  Config cfg;

try

  {

    cfg.readFile("example.cfg"); //读配置文件

  }

  catch(const FileIOException &fioex)

  {

    std::cerr << "I/O error while reading file." << std::endl;

    return(EXIT_FAILURE);

  }

  catch(const ParseException &pex)

  {

    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()

              << " - " << pex.getError() << std::endl;

    return(EXIT_FAILURE);

  }

// Get the store name.

  try

  {

    string name = cfg.lookup("name"); // 查询某个路径“name”,得到setting,存储到name

    cout << "Store name: " << name << endl << endl;

  }

  catch(const SettingNotFoundException &nfex)

  {

    cerr << "No 'name' setting in configuration file." << endl;

  }



  const Setting& root = cfg.getRoot();//得到根setting

 // Output a list of all books in the inventory.

  try

  {

    const Setting &books = root["inventory"]["books"];

    int count = books.getLength();   //得到某个setting的长度,从而循环读取


       for(int i = 0; i < count; ++i)

    {

      const Setting &book = books[i];

      // Only output the record if all of the expected fields are present.

      string title, author;

      double price;

      int qty;

      if(!(book.lookupValue("title", title)  //查询名字为title的setting存储到title中

           && book.lookupValue("author", author)

           && book.lookupValue("price", price)

           && book.lookupValue("qty", qty)))

        continue;


      cout << setw(30) << left << title << "  "

           << setw(30) << left << author << "  "

           << '$' << setw(6) << right << price << "  "

           << qty

           << endl;

    }

    cout << endl;

  }

  catch(const SettingNotFoundException &nfex)

  {

    // Ignore.

  }

  return(EXIT_SUCCESS);

}





// eof

例子二

头文件省略

// This example reads the configuration file 'example.cfg', adds a new

// movie record to the movies list, and writes the updated configuration to

// 'updated.cfg'.

int main(int argc, char **argv)

{

  static const char *output_file = "updated.cfg";



  Config cfg;

  // Read the file. If there is an error, report it and exit.

  try

  {

    cfg.readFile("example.cfg"); //读配置文件

  }

  catch(const FileIOException &fioex)

  {

    std::cerr << "I/O error while reading file." << std::endl;

    return(EXIT_FAILURE);

  }

  catch(const ParseException &pex)

  {

    std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()

              << " - " << pex.getError() << std::endl;

    return(EXIT_FAILURE);

  }

// Get the store name.

  try

  {

    string name = cfg.lookup("name");    //通过"name"这个路径查找 到某个setting(名字为 name) 存储到string name 中

    cout << "Store name: " << name << endl << endl;

  }

  catch(const SettingNotFoundException &nfex)

  {

    cerr << "No 'name' setting in configuration file." << endl;

  }





  // Find the 'movies' setting. Add intermediate settings if they don't yet

  // exist.

  Setting &root = cfg.getRoot(); //返回root setting

  if(! root.exists("inventory"))

    root.add("inventory", Setting::TypeGroup);

  Setting &inventory = root["inventory"];

  if(! inventory.exists("movies"))

    inventory.add("movies", Setting::TypeList);

  Setting &movies = inventory["movies"];

// Create the new movie entry.

  Setting &movie = movies.add(Setting::TypeGroup); //增加一个Setting::TypeGroup类型的子setting

  movie.add("title", Setting::TypeString) = "Buckaroo Banzai"; //增加一个子setting

  movie.add("media", Setting::TypeString) = "DVD";

  movie.add("price", Setting::TypeFloat) = 12.99;

  movie.add("qty", Setting::TypeInt) = 20;



  // Write out the updated configuration.

  try

  {

    cfg.writeFile(output_file); //把配置写到一个文件中

    cerr << "Updated configuration successfully written to: " << output_file

         << endl;

  }

  catch(const FileIOException &fioex)

  {

    cerr << "I/O error while writing file: " << output_file << endl;

    return(EXIT_FAILURE);

  }

  return(EXIT_SUCCESS);

}



// eof

配置文件:

example.cfg:

// An example configuration file that stores information about a store.



// Basic store information:

name = "Books, Movies & More";





// Store inventory:

inventory =

{

  books = ( { title  = "Treasure Island";

              author = "Robert Louis Stevenson";

              price  = 29.99;

              qty    = 5; },

            { title  = "Snow Crash";

              author = "Neal Stephenson";

              price  = 9.99;

              qty    = 8; }

          );





  movies = ( { title = "Brazil";

               media = "DVD";

               price = 19.99;

               qty = 11; },

             { title = "The City of Lost Children";

               media = "DVD";

               price = 18.99;

               qty = 5; },

             { title = "Memento";

               media = "Blu-Ray";

               price = 24.99;

               qty = 20;

             },

             { title = "Howard the Duck"; }

           );

};





// Store hours:

hours =

{

  mon = { open =  9; close = 18; };

  tue = { open =  9; close = 18; };

  wed = { open =  9; close = 18; };

  thu = { open =  9; close = 18; };

  fri = { open =  9; close = 20; };

  sat = { open =  9; close = 20; };

  sun = { open = 11; close = 16; };

};

libconfig第二篇----两个小例子的更多相关文章

  1. vuex2.0+两个小例子

    首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各核心概念的理解. 废话少说,直接上干货.这是官网上的一 ...

  2. Vuex2.0边学边记+两个小例子

    最近在研究Vuex2.0,搞了好几天终于有点头绪了. 首先vuex概念比较多,一定要搞懂里面的概念,可以参考官网Vuex2.0概念,我写此文的目的是希望能对前端爱好者提供个参考,加深对vuex2.0各 ...

  3. 学习HttpClient,从两个小例子开始

    前言 HTTP(Hyper-Text Transfer Protocol,超文本传输协议)在如今的互联网也许是最重要的协议,我们每天做的很多事情都与之有关,比如,网上购物.刷博客.看新闻等.偶尔你的上 ...

  4. 两个小例子彻底明白python decorator

    一:没有什么实际意思,就是单纯的理解decorator.使用装饰器完全可以阻止方法中的代码执行. class json_test(object): def __init__(self, *arg, * ...

  5. 关于Finereport移动端报表二次开发的两个小例子

    例1:刷新页面 1. 问题描述 A超链至B填报,B提交数据后返回A时,A自动刷新显示新的数据. 2. 解决方案 1. contentPane.setAppearRefresh();  //在A的加载结 ...

  6. Vue.js的小例子--随便写的

    1.领导安排明天给同事们科普下vue 2.简单写了两个小例子 3.话不多说直接上代码 <!DOCTYPE html> <html> <head> <meta ...

  7. python2.7练习小例子(二十九)

        29):1.题目:按相反的顺序输出列表的值. #!/usr/bin/python # -*- coding: UTF-8 -*- a = ['one', 'two', 'three'] for ...

  8. python2.7练习小例子(二十四)

        24):1.题目:利用递归方法求5!.     程序分析:递归公式:fn=fn_1*4! #!/usr/bin/python # -*- coding: UTF-8 -*- def fact( ...

  9. 小白两篇博客熟练操作MySQL 之 第二篇

    小白两篇博客熟练操作MySQL  之   第二篇 一. 视图 视图是一个虚拟表,其本质是根据SQL语句获取动态的数据集,并为其命名,用户使用时只需使用名称即可获取结果集, 并可以将其当做表来使用. s ...

随机推荐

  1. 8.3 sikuli 集成进eclipse 报错:eclipse中运行提示 Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform

    sikuli运行出现问题:Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform 在64位平台上无法加载32位的dll文件 解决办 ...

  2. java的两种异常runtimeException和checkedException

    java异常处理机制主要依赖于try,catch,finally,throw,throws五个关键字.   try 关键字后紧跟一个花括号括起来的代码块,简称try块.同理:下面的也被称为相应的块. ...

  3. public <T> void method(T var)

    今天项目中遇到这样一个方法: public <T> void method(T var) 不太明白什么意思,后来搜索下几篇文章,自己总结一下,和大家分享. 先看例子: 1.static v ...

  4. elasticsearch 性能调优

    所有的修改都可以在elasticsearch.yml里面修改,也可以通过api来修改.推荐用api比较灵活 1.不同分片之间的数据同步是一个很大的花费,默认是1s同步,如果我们不要求实时性,我们可以执 ...

  5. 6--OC--封装 继承 多态

    OC中的类的三大特性类: 继承,封装,多态 一. 封装 封装就是对类中的一些字段,方法进行保护,不被外界所访问到,有一种权限的控制功能,这样我们在定义类的时候,哪些字段和方法不想暴露出去,哪些字段和方 ...

  6. 滑轮关节(b2PulleyJoint)

    package{ import Box2D.Collision.b2AABB; import Box2D.Collision.b2RayCastInput; import Box2D.Collisio ...

  7. android性能优化优秀文章

    郭霖最近整理的文章: 合理管理内存 分析编码过程中如何避免过多内存占用,以及如何实现高性能的内存使用. 尽可能使用intentService; 当界面不可见时释放内存; 当内存紧张时释放内存; 避免b ...

  8. JavaScript判断数组是否存在key

    JS中复合数组associative array和对象是等同的,判断一个key是否存在于数组中(或对象是否包含某个属性),不能使用ary[key] == undefined,因为可能存在ary = { ...

  9. POJ 1821 单调队列+dp

    题目大意:有K个工人,有n个墙,现在要给墙涂色.然后每个工人坐在Si上,他能刷的最大范围是Li,且必须是一个连续子区间,而且必须过Si,他刷完后能获得Pi钱 思路:定义dp[i][j]表示前i个人,涂 ...

  10. css预处理器

    Sass.LESS是什么?大家为什么要使用他们?  他们是CSS预处理器.他是CSS上的一种抽象层.他们是一种特殊的语法/语言编译成CSS.  Less是一种动态样式语言. 将CSS赋予了动态语言的特 ...