使用C ++拆分在“[一般设置]”中的段字符串,格式 [英] Using C++ splitting a section string which is in "[General Setting]" format

查看:158
本文介绍了使用C ++拆分在“[一般设置]”中的段字符串,格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我想读取ini文件,它有section和键 - 值对。
根据节,我想读取相应键的值。
首先,我想阅读括在方括号中的部分。
请帮助。
谢谢。

I am new to C++, i want to read ini file which has section and key - value pair. Depending on the section, i want to read the value for corresponding key. Primarily, i want to read the section which is enclosed in square brackets. Please help. Thank you.

推荐答案

对于真正的INI文件解析,我强烈建议 iniparser库

For real INI file parsing, I highly suggest the iniparser library. It is excellently documented and easy to use in both C and C++ programs.

如果您只对解析字符串<$ p 有兴趣, c $ c> [Section name] ,可以执行以下操作:为字符串查找第一个'['和最后']',并标记位置。如果已找到这两个字符,请将节名称作为您确定的位置之间的子字符串。

If you are only interested in parsing strings of the form [Section name], you could do the following: Find first '[' and last ']' for the string and mark the positions. If both characters have been found, take the section name to be substring between the positions you identified.

假设您使用 std :: string ,您可以执行以下操作:

Assuming you are using an std::string, you can do the following:

std::string myString = " [Section name] ";

std::size_t start = myString.find_first_of( '[' );
std::size_t end   = myString.find_last_of( ']' );

std::string sectionName;
if( start != std::string::npos && end != std::string::npos )
{
  sectionName = myString.substr(start + 1, end - 1);
}

std::cout << sectionName << std::endl;

这篇关于使用C ++拆分在“[一般设置]”中的段字符串,格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆