Reputation: 25
I have a file which looks like:
123,Cheese,Butter
78,Milk,Vegetable,Fish
and I wish to read each line into a data type List
which has int num
and char things[3][10]
using overloaded operator >>. So far I have:
friend istream& operator>> (istream &is, List &rhs)
{
char comma;
is >> rhs.num >> comma >> ... (I don't know how to continue)
return is;
} // operator>>
Am I doing it right using char comma
to skip a comma? How do I read different entries with different lengths separated by comma without using string?
Upvotes: 1
Views: 93
Reputation: 13988
It will be only a pseudocode but if you really need to avoid std::string your best choice is to make it more or less look like this:
istream &operator >>(istream &s, YourType &mylist) {
char mybuf[256];
s.read(mybuf, 256);
char *beg = mybuf;
char *cur = beg;
while (cur != mybuf + 256 && *cur!=0) {
if (*cur == '\n') {
mylist.addnext();
}
if (*cur == ',') {
*cur = 0; //to make the char string end on each comma
mylist.current.add(beg);
beg = cur + 1;
}
}
}
Remember that if YourType
will be for example vector<vector<const char *>>
you will need to add the operator >>
into the std namespace.
Upvotes: 1