It seems that the idiomatic version of C ++ is:
using namespace std; istream& getrecord(istream& s, REQUEST& req) { string part; bool result = getline(s, part, ',') && (istringstream(part) >> req.asu) && getline(s, part, ',') && (istringstream(part) >> req.lba) && getline(s, part, ',') && (istringstream(part) >> req.size) && getline(s, part, ',') && ((part.size() == 1) ? (req.opcode = part[0], true) : false) && getline(s, part, ',') && (istringstream(part) >> req.timestamp); if (!result && s) s.setstate(ios::failbit); return s; } int main() { string original("0,303567,3584,w,0.000000"); REQUEST req; istringstream origs(original); if (getrecord(origs, req)) cout << "asu = " << req.asu << ", lba = " << req.lba << ", size = " << req.size << ", opcode = " << req.opcode << ", timestamp = " << req.timestamp; else cout << "FAILED"; }
Unfortunately, stringstream can not read in char , so it turned out not so beautiful.
Check: http://ideone.com/IPtprZ