| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> C++知识库 -> 【C/C++】如何解析一个简单的配置文件|解析配置文件代码 -> 正文阅读 |
|
|
[C++知识库]【C/C++】如何解析一个简单的配置文件|解析配置文件代码 |
How to parse a simple config file本文解释了如何解析类似于 Windows .ini 文件的 name=value 形式的配置文件。该代码从行中删除所有空格并跳过空行和包含注释的行。 配置文件的格式The code explained in this article can parse both a formatted and unformatted input
实施摘要下面的代码解析配置文件。在我们成功加载文件后,我们一行一行地读取它。我们从该行中删除所有空格,如果该行为空或包含注释(用“#”表示),则跳过该行。之后,我们在分隔符“=”处拆分字符串“name=value”并打印名称和值。
我看到的唯一问如果非注释行不包含 '=' 字符,代码将不起作用。第 14 行删除了该行中的所有空格,但这就是两边是空的,应该判断一下。(http://www.cplusplus.com/forum/general/266880/) 实现细节 1 - 去除空格在进行任何进一步处理之前,我们从行中删除所有空格。这是在几个函数的帮助下完成的,即erase()、remove_if() 和 isspace。
remove_if() The function remove_if() takes a sequence (i.e. the line) and transforms it into a sequence without the undesired characters (i.e. the whitespaces). The length of the sequence does not get altered, however, the elements representing the undesired characters are moved to the end of the sequence and remain in an unspecified state. The function returns an iterator to the new end of the sequence. This is illustrated below.?remove_if()?takes three arguments. The first two arguments are forward iterators to the initial and final positions in the sequence. The last argument is a function pointer or a function object (in our case the address of the function isspace).
isspace The function isspace checks whether an individual character is a whitespace character. Behind the scenes, isspace accepts a single element of the sequence as an argument and returns a value convertible to bool, i.e. true or false.
Note:?In "C" locale whitespace characters include the space (’ ’), form feed (’\f’), line feed (’\n’), carriage return (’\r’), horizontal tab (’\t’), and vertical tab (’\v’). The backspace character (’\b’) is not a whitespace character in "C" locale. Different locales might define other whitespace characters. From C++ In a Nutshell: A Desktop Quick Reference By Ray Lischner. string::erase() The method std::string::erase() erases the sequence of characters in the range (first, last). In our case, it removes the part between the new end of the sequence returned by std::remove_if and the original end of the sequence. This can be seen in the figure below.
Implementation detail 2 - Splitting a string at the delimiterThe code below splits the line at the delimiter =. Notice, that we are processing the line under the assumption that there are no whitespace characters, as they have all been removed in the previous step. We firstly find the position of the delimiter = with?std::string::find(). After that, we use the method?std::string::substr(pos, len)?to extract the name and the value. The method substr(pos,len) creates a substring starting at the position pos and spans len characters (or until the end of the string, whichever comes first). Notice, that in the second case we pass only the first parameter, i.e. the position. The default value, i.e. all characters until the end of the string, is used as the second parameter.
|
|
|
| C++知识库 最新文章 |
| 【C++】友元、嵌套类、异常、RTTI、类型转换 |
| 通讯录的思路与实现(C语言) |
| C++PrimerPlus 第七章 函数-C++的编程模块( |
| Problem C: 算法9-9~9-12:平衡二叉树的基本 |
| MSVC C++ UTF-8编程 |
| C++进阶 多态原理 |
| 简单string类c++实现 |
| 我的年度总结 |
| 【C语言】以深厚地基筑伟岸高楼-基础篇(六 |
| c语言常见错误合集 |
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/20 2:02:25- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |