gpt4 book ai didi

c++ - 解析一个非常简单的配置文件

转载 作者:行者123 更新时间:2023-11-28 02:44:26 27 4
gpt4 key购买 nike

我正在用 C++ 编写 OBDII 读取库/应用程序。通过发送一个简单的字符串命令从汽车的计算机中检索数据,然后将结果传递给每个参数特定的函数。

我想读取我想要的所有命令的配置文件,可能是这样的:

Name, Command, function

Engine RPM, 010C, ((256*A)+B)/4
Speed, 010D, A

基本上,非常简单,所有数据只需要作为字符串读入即可。谁能为此推荐一个好的简单库?如果重要的话,我的目标是 Linux 上的 g++ 和/或 Clang。

最佳答案

您可以使用 std::ifstream 逐行阅读,并使用 boost::split 拆分行,

示例代码:

您可以检查 token 大小以对加载的文件进行完整性检查。

#include <fstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

int main(int argc, char* argv[]) {
std::ifstream ifs("e:\\save.txt");

std::string line;
std::vector<std::string> tokens;
while (std::getline(ifs, line)) {
boost::split(tokens, line, boost::is_any_of(","));
if (line.empty())
continue;

for (const auto& t : tokens) {
std::cout << t << std::endl;
}
}

return 0;
}

你也可以使用 String Toolkit Library如果你不想实现。 Docs

关于c++ - 解析一个非常简单的配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918394/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com