C ++ reading generated tabular data from a file using BOOST / STL / etc

What is the best way to create a formatted read / write data function for a tabular text file. Say a few calls like:

readElement(i,j)

insertRow(elem[])

readColHeaders()

I was wondering if any existing shell can do this?

internal format - tab delimited data or CSV.

Thnx-egon

+3
source share
4 answers

There are many csv readers, but I never found something nice.

The easiest way is to use boost :: tokenize to populate the vector <vector <string β†’ from your file. It’s best to use boost :: spirit (but the learning curve is a roller coaster).

To generate a sur file, iterating over the vector <vector <string β†’ is pretty trivial.

+3

/ csv C ++. , - , , . csv, , . - , , , :

  • boost:: any. , , , scanf. . boost:: tokenize boost:: lexical_cast . , csv , .

  • get(), any_cast .

  • , ,

  • " " , , β†’ . , , CUSIP , , .

  • , ()

  • , , , /.

  • , /

  • ,

, , perl, , 10 10 perl. csv ++, .

+1

...

#include <vector>
#include <string>
#include <sstream>
#include <iostream>

typedef std::vector<std::string> StringVec;
typedef std::vector<StringVec> RowVec;

RowVec readRows(std::istream& f) {
    std::string line;
    RowVec rows;
    while (std::getline(f, line)) {
        rows.push_back(StringVec());
        std::string entry;
        std::istringstream linestrm(line);
        while (std::getline(linestrm, entry, '\t')) {
            rows.back().push_back(entry);
        }
    }
    return rows;
}

int main() {
    std::istringstream textFile("a\tb\tc\n1\t2\t3");
    RowVec rows = readRows(textFile);
    std::cout << rows.size() << std::endl;
    std::cout << rows[0][0] << std::endl;
    std::cout << rows[1][2] << std::endl;
    return 0;
}
+1

(, ), . boost::numeric::ublas::matrix<std::string> std::vector<std::vector<std::string> >

Boost.Spirit . , :

boost::spirit::qi::phrase_parse(
    begin,
    end, 
    // parse rule:

        *(char_ - '\t') % '\t' 

    // end parse rule
    space,
    vec);`

: http://www.boost.org/doc/libs/1_46_0/libs/spirit/doc/html/spirit/qi/tutorials.html

0

Source: https://habr.com/ru/post/1794438/


All Articles