C ++如何读取CFG文件更复杂

c++ how to read a cfg file more complex?

本文关键字:CFG 文件 复杂 读取 何读取      更新时间:2023-10-16

我最近从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 件事:

  1. 行,我们可以getline阅读
  2. 键(=之前行的部分)
  3. 值(=后面的部分)。

    我们可以通过查找=字符来拆分键和值。

所以,你会得到类似的东西:

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