C ++如何读取CFG文件更复杂
c++ how to read a cfg file more complex?
我最近从c#改成了c++。我一直在看一些关于如何读取配置文件的教程。我可能问错了,我的意思是:我正在制作一个程序,但它适用于多个用户。每个用户都会对文本文件中的内容有自己的预设。例:在我的文本文件("items.txt")中,我有默认的"整数"和可配置的"整数"。
10=5
90=2
50=9
在 c# 中,如果我没记错的话,我读取了所有行,如果该行以例如"10="开头,我拆分了文本,所以我的行只能是可配置的 int left,我可以在我的程序中轻松使用它,它是这样的:
string[] lines = File.ReadAllLines(path);
string str;
foreach (string line in lines)
{
if (line.StartsWith("10"))
{
str = line.Split('=')[1];
//I have what i need (str);
}
}
我这样做是为了我需要的一切。那么,在 c++ 中执行此操作的最佳方法是什么?
另外:我需要为它们获取特定的行,以便以后可以在程序中全部使用它们。
提前非常感谢!
您的文件中有 3 件事:
- 行,我们可以
getline
阅读 - 键(
=
之前行的部分) -
值(
=
后面的部分)。我们可以通过查找
=
字符来拆分键和值。
所以,你会得到类似的东西:
std::ifstream file(path);
if (!file) {
std::cerr << "unable to open " << path << std::endl;
return false;
}
for (std::string line; getline(file, line); ) {
auto pos = line.find('=');
if (pos == line.end()) {
std::cerr << "skipping invalid line '" << line << "'n";
continue;
}
auto key = line.substr(0, pos);
auto val = line.substr(pos);
// do something with key,val
}
如果要将上面的键和值字符串转换为int
,可以使用std::stringstream
进行转换,也可以使用std::strtol
。
此代码与您发布的 C# 代码C++等效。这段代码本身会编译,当然你必须填写一些东西 for path 而不是一个空字符串。如果你在方法中的某个地方需要它而不是作为一个程序本身,放弃int main()并返回0。如果有什么不清楚的地方,请回复这个。我尝试以与 C# 代码相同的结构方式设置它,以便您了解所有内容。
#include <fstream>
#include <vector>
#include <string>
int main()
{
std::string path = "";
std::ifstream file(path); //open the file from path, might want to check file.fail()
std::vector<std::string> lines;
std::string line;
while (file >> line)
lines.push_back(line);
std::string str;
for (const std::string& line : lines)
{
if (line.substr(0, 2).compare("10") == 0)
{
size_t foundIdx = line.find('=');
str = line.substr(foundIdx + 1);
}
}
file.close();
return 0;
}
在C++中,我们现在使用 std::regex
对于您的情况 - 我会使用以下:
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
int main()
{
// Simple config file
std::string rules = "10=5n 90=2n50=9";
std::regex single_rule_regex("[0-9]+\=[0-9]+");
std::regex single_rule_part_regex("[0-9]+");
auto rules_begin = std::sregex_iterator(rules.begin(),
rules.end(),
single_rule_regex);
auto rules_end = std::sregex_iterator();
for (auto it = rules_begin; it != rules_end; ++it)
{
std::smatch rule_match = *it;
std::string rule_str = rule_match.str();
std::cout << rule_str << std::endl;
// key=value
auto parts = std::sregex_iterator(rule_str.begin(),
rule_str.end(),
single_rule_part_regex);
std::string key = parts->str();
std::string value = (++parts)->str();
std::cout << key << " has " << value << std::endl;
}
}
给定rules
的输出为:
10=5
10 有 5
90=2
90 有 2
50=9
50 有 9
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 在C++中查找文件
- c++库的公共头文件中应该包含什么
- 用c++从输入文件中读取另一行
- 在OpenCV DNN中解析cfg文件froom YOLO-Darknet
- 食人魔异常(6:文件未找到异常):找不到'ogre.cfg'文件
- 如何在.cfg.txt文件中读取
- 如何在 INI、CFG 或 XML 文件中存储C++变量
- C ++如何读取CFG文件更复杂