C++ - 如何使用流来解析文件?

7 c++ iostream

我有一个文件,我需要循环它分配一个int foo,字符串类型,64/128位长.我如何使用流将这些行解析为以下变量 - 我想坚持使用流语法(ifs >> foo >> type)但在这种情况下类型最终将成为0之后的其余行52 ...在那一点上我只得到一个char*并使用strtoull等等,所以为什么首先使用流...我希望可读代码没有可怕的性能而不是字符串/ strtok/strtoull

//input file:
0ULL'04001C0180000000000000000EE317BC'
52L'04001C0180000000'
//ouput:
//0 ULL 0x04001C0180000000 0x000000000EE317BC
//52 L 0x04001C0180000000

  ifstream ifs("input.data");
  int foo;
  string type;
  unsigned long long ull[2];
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 8

提升精神实施

这是强制性的基于Boost Spirit(Qi)的实现.为了更好的衡量,包括使用Boost Spirit(Karma)进行格式化:

#include <string>
#include <iostream>
#include <fstream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>

namespace karma=boost::spirit::karma;
namespace qi   =boost::spirit::qi;

static qi::uint_parser<unsigned long long, 16, 16, 16> hex16_p; // parse long hex
static karma::uint_generator<unsigned long long, 16>   hex16_f; // format long hex

int main(int argc, char** args)
{
    std::ifstream ifs("input.data");
    std::string line;
    while (std::getline(ifs, line))
    {
        std::string::iterator begin = line.begin(), end = line.end();

        int                             f0;
        std::string                     f1;
        std::vector<unsigned long long> f2;

        bool ok = parse(begin, end,
                qi::int_                    // an integer
                >> *qi::alpha               // alternatively: *(qi::char_ - '\'')
                >> '\'' >> +hex16_p >> '\'' // accepts 'n x 16' hex digits
            , f0, f1, f2);

        if (ok)
            std::cout << "Parsed: " << karma::format(
                 karma::int_ 
                 << ' ' << karma::string 
                 << ' ' << ("0x" << hex16_f) % ' '
             , f0, f1, f2) << std::endl;
        else
            std::cerr << "Parse failed: " << line << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

测试运行:

Parsed: 0 ULL 0x4001c0180000000 0xee317bc
Parsed: 52 L 0x4001c0180000000
Run Code Online (Sandbox Code Playgroud)

有关如何调整例如十六进制输出的信息,请参阅下面的调整和示例

基准

我已经对@ Cubbi的版本及以上内容进行了基准测试,编写100,000x您提供的样本输入.这最初给Cubbi的版本略有优势:0.786s0.823s.

现在,这当然不公平比较,因为我的代码每次都在动态构建解析器.这样就取消了循环:

typedef std::string::iterator It;

const static qi::rule<It> parser = qi::int_ >> *qi::alpha >> '\'' >> +hex16_p >> '\'';
bool ok = parse(begin, end, parser, f0, f1, f2);
Run Code Online (Sandbox Code Playgroud)

提升精神只是一个明显的赢家0.093s; 已经快了8.5倍,即使每次迭代仍然构造了业力格式器.

两个版本中输出格式都被注释掉了,Boost Spirit的速度提高了11倍

调整,样品

请注意如何轻松调整内容:

//  >> '\'' >> +hex16_p >> '\'' // accepts 'n x 16' hex digits
    >> '\'' >> qi::repeat(1,2)[ hex16_p ] >> '\'' // accept 16 or 32 digits
Run Code Online (Sandbox Code Playgroud)

或者像输入一样格式化十六进制输出:

// ("0x" << hex16_f) % ' '
karma::right_align(16, '0')[ karma::upper [ hex16_f ] ] % ""
Run Code Online (Sandbox Code Playgroud)

更改了样本输出:

0ULL'04001C0180000000000000000EE317BC'
Parsed: 0 ULL 04001C0180000000000000000EE317BC
52L'04001C0180000000'
Parsed: 52 L 04001C0180000000
Run Code Online (Sandbox Code Playgroud)

HTH


Cub*_*bbi 4

对于像boost.spirit这样更复杂的解析器来说,这是一个相当简单的任务。

使用标准 C++ 流来解决此问题,您需要

  • a) 视为'空白并且
  • b) 对字符串“04001C0180000000000000000EE317BC”进行额外传递,该字符串的值之间没有分隔符。

借用 Jerry Coffin 的示例方面代码

#include <iostream>
#include <fstream>
#include <locale>
#include <vector>
#include <sstream>
#include <iomanip>
struct tick_is_space : std::ctype<char> {
    tick_is_space() : std::ctype<char>(get_table()) {}
    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask>
               rc(table_size, std::ctype_base::mask());
        rc['\n'] = std::ctype_base::space;
        rc['\''] = std::ctype_base::space;
        return &rc[0];
    }
};

int main()
{
    std::ifstream ifs("input.data");
    ifs.imbue(std::locale(std::locale(), new tick_is_space()));
    int foo;
    std::string type, ullstr;
    while( ifs >> foo >> type >> ullstr)
    {
        std::vector<unsigned long long> ull;
        while(ullstr.size() >= 16) // sizeof(unsigned long long)*2
        {
            std::istringstream is(ullstr.substr(0, 16));
            unsigned long long tmp;
            is >> std::hex >> tmp;
            ull.push_back(tmp);
            ullstr.erase(0, 16);
        }
        std::cout << std::dec << foo << " " << type << " "
                  << std::hex << std::showbase;
        for(size_t p=0; p<ull.size(); ++p)
            std::cout << std::setw(16) << std::setfill('0') << ull[p] << ' ';
        std::cout << '\n';
    }
}
Run Code Online (Sandbox Code Playgroud)

测试: https: //ideone.com/lRBTq