I am trying to read a file where each line contains data separated by commas. I passing each line to a istringstream object to treat that line as a stream. Now I would like to write a custom stream manipulator that reads data until it hits a comma or eof.
istringstream ins;
ins.str("hi , 32.5 , , 43");
string s;
my_read(s,ins);
ins >> s;
cout << s << endl;
double val;
my_read(s,ins);
ins >> val;
cout << val << endl;
my_read(s,ins);
ins >> s;
cout << s << endl;
I don't know if this is a proper way, but either way it has a problem if there are empty fields like in the 3rd attempt to read, in that case the read string is "," where is should be empty. How can I solve this problem?. Also please note that I only want to use stl and no external libraries.
I have to admit that I'd prob just use std::string's functionality to deal with each line (you did say that you were reading your file a line at a time, into a std::string, and then using that to initialize a std::istringstream?)
What do you want to do with the extracted values, by the way. I assume it's more that displaying them.
I think a istringstream based solution would only likely be a curiousity...
Andy
PS What you talking about is a stream extraction operator, not a manipulator (those are things like std::setw(), std::hex(), as defined in <iomanip>)