
Hi, I have to parse the file, the contents of which(with a header) is below and stuff the double part only to std::vector<double>: "A","B","C","D" 1,2,3,4 5,6.7,8 ........ I started simple without header approach first : from libs/spirit/example/lex/example.hpp: inline std::string read_from_file(char const* infile) { std::ifstream instream(infile); if (!instream.is_open()) { std::cerr << "Couldn't open file: " << infile << std::endl; exit(-1); } instream.unsetf(std::ios::skipws); // No white space skipping! return std::string(std::istreambuf_iterator<char>(instream.rdbuf()), std::istreambuf_iterator<char>()); } from libs/spirit/classic/examples/number_list.hpp: /////////////////////////////////////////////////////////////////////////////// // // Our comma separated list parser // /////////////////////////////////////////////////////////////////////////////// bool parse_numbers(char const* str, vector<double>& v) { return parse(str, // Begin grammar ( real_p[push_back_a(v)] >> *(',' >> real_p[push_back_a(v)]) ) , // End grammar space_p).full; } int main() { std::string str = read_from_file("test.csv"); std::vector<double> v; if (parse_numbers(str.c_str(), v)) { cout << "-------------------------\n"; cout << "Parsing succeeded\n"; cout << str << " Parses OK: " << endl; for (vector<double>::size_type i = 0; i < v.size(); ++i) cout << i << ": " << v[i] << endl; cout << "-------------------------\n"; } else { cout << "-------------------------\n"; cout << "Parsing failed\n"; cout << "-------------------------\n"; } } But after running it, I am getting the result as "Parsing failed". I am going thru the doc to understand it better, but any quick spot will help me immensely. Thanks, Chandra

I have to parse the file, the contents of which(with a header) is below and stuff the double part only to std::vector<double>:
"A","B","C","D" 1,2,3,4 5,6.7,8 ........
Oops, there were typos .. the file reads like this: "A","B","C","D" 1.0,2.0,3.0,4.0 5.0,6.0,7.0,8.0 I have still to find the solution though :(

Chandrashekhar Kumar wrote:
I have to parse the file, the contents of which(with a header) is below and stuff the double part only to std::vector<double>:
"A","B","C","D" 1,2,3,4 5,6.7,8 ........
Oops, there were typos ..
the file reads like this:
"A","B","C","D" 1.0,2.0,3.0,4.0 5.0,6.0,7.0,8.0
I have still to find the solution though :(
BTW, you might want to post to Spirit's mailing list: http://www.boost.org/community/groups.html#spirit Please post complete code that we can try, as per Spirit's policy: http://www.nabble.com/Support-Policy-td21433231.html#a21433231 Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net

Chandrashekhar Kumar wrote:
Hi,
I have to parse the file, the contents of which(with a header) is below and stuff the double part only to std::vector<double>:
"A","B","C","D" 1,2,3,4 5,6.7,8 ........
<snip> bool parse_numbers(char const* str, vector<double>& v) { return parse(str,
// Begin grammar ( real_p[push_back_a(v)] >> *(',' >> real_p[push_back_a(v)]) ) , // End grammar
space_p).full; } <snip>
But after running it, I am getting the result as "Parsing failed".
I am going thru the doc to understand it better, but any quick spot will help me immensely.
The string that you are parsing begins "A","B","C",... Naturally, it doesn't match a grammar that parses a list of numbers. In Christ, Steven Watanabe

bool parse_numbers(char const* str, vector<double>& v) { return parse(str,
// Begin grammar ( real_p[push_back_a(v)] >> *(',' >> real_p[push_back_a(v)]) ) , // End grammar
space_p).full; } <snip>
But after running it, I am getting the result as "Parsing failed".
I am going thru the doc to understand it better, but any quick spot will help me immensely.
The string that you are parsing begins "A","B","C",...
Naturally, it doesn't match a grammar that parses a list of numbers.
In Christ, Steven Watanabe
But it is not working for the below as well: 1.0,2.0,3.0,4.0 5.0,6.0,7.0,8.0

Chandrashekhar Kumar wrote:
But it is not working for the below as well:
1.0,2.0,3.0,4.0 5.0,6.0,7.0,8.0
The question why this doesn't work can be simplified to the question why bool matches = parse ("1.0,2.0,3.0,4.0\n5.0,6.0,7.0,8.0", real_p >> *(',' >> real_p) , space_p).full; will result in matches being "false". I think the reason is that the skip parser "space_p" will eat the "\n" between "4.0" and "5.0", but it will not insert a ','. A potential solution might be to make ',' optional by writing !',': bool matches = parse ("1.0,2.0,3.0,4.0\n5.0,6.0,7.0,8.0", real_p >> *(!',' >> real_p) , space_p).full; will probably result in matches being "true". Regards, Thomas
participants (4)
-
Chandrashekhar Kumar
-
Joel de Guzman
-
Steven Watanabe
-
Thomas Klimpel