Search code examples
c++boosttokenize

boost tokenizer / char separator


I have tried with both commented and uncomented version of the code:

string separator1(""); //dont let quoted arguments escape themselves
string separator2(",\n"); //split on comma and newline
string separator3("\"\'"); //let it have quoted arguments

escaped_list_separator<char> els(separator1, separator2, separator4);
tokenizer<escaped_list_separator<char>> tok(str);//, els);


for (tokenizer<escaped_list_separator<char>>::iterator beg = tok.begin();beg!= tok.end(); ++beg) {
next = *beg;
boost::trim(next);
cout << counter << " " << next << endl;
counter++;
}

to separate a file which has the following format:

 12345, Test Test, Test
 98765, Test2 test2, Test2

This is the output

0 12345
1 Test Test
2 Test
98765
3 Test2 test2
4 Test2

I am not sure where the problem is but what I need to achieve is to have a number 3 before 98765


Solution

  • You forgot the newline separator: string separator2(",\n");

    #include <iostream>
    #include <boost/tokenizer.hpp>
    #include <boost/algorithm/string.hpp>
    
    using namespace std;
    
       using namespace boost;
    
    int main() {
        string str = "TEst,hola\nhola";
        string separator1(""); //dont let quoted arguments escape themselves
        string separator2(",\n"); //split on comma and newline
        string separator3("\""); //let it have quoted arguments
    
        escaped_list_separator<char> els(separator1, separator2, separator3);
        tokenizer<escaped_list_separator<char>> tok(str, els);
    
        int counter = 0, current_siding = 0, wagon_pos = 0, cur_vector_pos = 0;
    
        string next;
    
        for (tokenizer<escaped_list_separator<char>>::iterator beg = tok.begin();     beg != tok.end(); ++beg) {
            next = *beg;
            boost::trim(next);
            cout << counter << " " << next << endl;
            counter++;
    
        }
        return 0;
    }