解析非常简单的配置文件 [英] Parsing a Very Simply Config File

查看:173
本文介绍了解析非常简单的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 名称,命令,函数

引擎RPM ,010C,((256 * A)+ B)/ 4
速度,010D,A

基本上,非常简单,所有数据都需要以字符串形式读入。任何人都可以推荐一个好的简单的库为此?

解决方案

您可以使用 std :: ifstream 逐行读取 boost :: split p>

示例代码:



您可以检查令牌大小以检查加载的文件的完整性。

  #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>令牌
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 如果你不想实现。 文档


I am writing a OBDII reading library / application in C++. Data is retrieved from the car's computer by sending a simple string command, then passing the result through a function specific to each parameter.

I would like to read a config file of all the commands I want, something like this perhaps:

Name, Command, function

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

Basically, very simple, all data needs to just be read in as a string. Can anyone recommend a good simple library for this? My target is g++ and/ or Clang on Linux if that matters.

解决方案

You could use std::ifstream to read line by line, and boost::split to split the line by ,.

Sample code:

You could check tokens size for sanity checks of file loaded.

#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;
}

You could also use String Toolkit Library if you don't want to implemented. Docs

这篇关于解析非常简单的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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