<i id='R5Hno'><tr id='R5Hno'><dt id='R5Hno'><q id='R5Hno'><span id='R5Hno'><b id='R5Hno'><form id='R5Hno'><ins id='R5Hno'></ins><ul id='R5Hno'></ul><sub id='R5Hno'></sub></form><legend id='R5Hno'></legend><bdo id='R5Hno'><pre id='R5Hno'><center id='R5Hno'></center></pre></bdo></b><th id='R5Hno'></th></span></q></dt></tr></i><div id='R5Hno'><tfoot id='R5Hno'></tfoot><dl id='R5Hno'><fieldset id='R5Hno'></fieldset></dl></div>
  • <small id='R5Hno'></small><noframes id='R5Hno'>

      • <bdo id='R5Hno'></bdo><ul id='R5Hno'></ul>

    1. <legend id='R5Hno'><style id='R5Hno'><dir id='R5Hno'><q id='R5Hno'></q></dir></style></legend>
    2. <tfoot id='R5Hno'></tfoot>

      1. 用 C++ 创建一个简单的配置文件和解析器

        时间:2023-10-17

        <i id='bQ5BP'><tr id='bQ5BP'><dt id='bQ5BP'><q id='bQ5BP'><span id='bQ5BP'><b id='bQ5BP'><form id='bQ5BP'><ins id='bQ5BP'></ins><ul id='bQ5BP'></ul><sub id='bQ5BP'></sub></form><legend id='bQ5BP'></legend><bdo id='bQ5BP'><pre id='bQ5BP'><center id='bQ5BP'></center></pre></bdo></b><th id='bQ5BP'></th></span></q></dt></tr></i><div id='bQ5BP'><tfoot id='bQ5BP'></tfoot><dl id='bQ5BP'><fieldset id='bQ5BP'></fieldset></dl></div>

              <tbody id='bQ5BP'></tbody>
          1. <legend id='bQ5BP'><style id='bQ5BP'><dir id='bQ5BP'><q id='bQ5BP'></q></dir></style></legend><tfoot id='bQ5BP'></tfoot>
              <bdo id='bQ5BP'></bdo><ul id='bQ5BP'></ul>
            • <small id='bQ5BP'></small><noframes id='bQ5BP'>

                • 本文介绍了用 C++ 创建一个简单的配置文件和解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个看起来像这样的简单配置文件

                  I am trying to create a simple configuration file that looks like this

                  url = http://mysite.com
                  file = main.exe
                  true = 0
                  

                  当程序运行时,我希望它将配置设置加载到下面列出的程序变量中.

                  when the program runs, I would like it to load the configuration settings into the programs variables listed below.

                  string url, file;
                  bool true_false;
                  

                  我做了一些研究,这个链接似乎有帮助(nucleon 的帖子),但我似乎无法让它工作,而且我的理解太复杂了.有没有一种简单的方法可以做到这一点?我可以使用 ifstream 加载文件,但这是我自己所能得到的.谢谢!

                  I have done some research and this link seemed to help (nucleon's post) but I can't seem to get it to work and it is too complicated to understand on my part. Is there a simple way of doing this? I can load the file using ifstream but that is as far as I can get on my own. Thanks!

                  推荐答案

                  一般来说,解析此类典型的配置文件最容易分两个阶段:首先读取行,然后逐行解析.
                  在 C++ 中,可以使用 std::getline() 从流中读取行.虽然默认情况下它会读到下一个 ' '(它会消耗,但不会返回),你也可以给它传递一些其他的分隔符,这使它成为阅读的好选择up-to-some-char,比如你的例子中的 = .

                  In general, it's easiest to parse such typical config files in two stages: first read the lines, and then parse those one by one.
                  In C++, lines can be read from a stream using std::getline(). While by default it will read up to the next ' ' (which it will consume, but not return), you can pass it some other delimiter, too, which makes it a good candidate for reading up-to-some-char, like = in your example.

                  为简单起见,以下假设 = not 被空格包围.如果你想在这些位置允许空格,你必须有策略地放置 is >>std::ws 在读取值并从键中删除尾随空格之前.但是,对于配置文件阅读器来说,IMO 在语法上增加的一点灵活性并不值得.

                  For simplicity, the following presumes that the = are not surrounded by whitespace. If you want to allow whitespaces at these positions, you will have to strategically place is >> std::ws before reading the value and remove trailing whitespaces from the keys. However, IMO the little added flexibility in the syntax is not worth the hassle for a config file reader.

                  const char config[] = "url=http://example.com
                  "
                                        "file=main.exe
                  "
                                        "true=0";
                  
                  std::istringstream is_file(config);
                  
                  std::string line;
                  while( std::getline(is_file, line) )
                  {
                    std::istringstream is_line(line);
                    std::string key;
                    if( std::getline(is_line, key, '=') )
                    {
                      std::string value;
                      if( std::getline(is_line, value) ) 
                        store_line(key, value);
                    }
                  }
                  

                  (添加错误处理留给读者作为练习.)

                  这篇关于用 C++ 创建一个简单的配置文件和解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:取消一个deadline_timer,无论如何都会触发回调 下一篇:如何配置鼠标以编程方式提高指针精度

                  相关文章

                • <small id='Qp09z'></small><noframes id='Qp09z'>

                  • <bdo id='Qp09z'></bdo><ul id='Qp09z'></ul>
                  <tfoot id='Qp09z'></tfoot>

                      <i id='Qp09z'><tr id='Qp09z'><dt id='Qp09z'><q id='Qp09z'><span id='Qp09z'><b id='Qp09z'><form id='Qp09z'><ins id='Qp09z'></ins><ul id='Qp09z'></ul><sub id='Qp09z'></sub></form><legend id='Qp09z'></legend><bdo id='Qp09z'><pre id='Qp09z'><center id='Qp09z'></center></pre></bdo></b><th id='Qp09z'></th></span></q></dt></tr></i><div id='Qp09z'><tfoot id='Qp09z'></tfoot><dl id='Qp09z'><fieldset id='Qp09z'></fieldset></dl></div>

                    1. <legend id='Qp09z'><style id='Qp09z'><dir id='Qp09z'><q id='Qp09z'></q></dir></style></legend>