If you have control over the format, it'll be (slightly) easier to read if you eliminate the commas, and just have input like
e 225 370 35 75
With this format, Poita_'s code for reading the data will work [edit: he's since update his code to explicitly read and skip the commas]. Otherwise, you'll need to explicitly skip over the commas:
char ingore1, ignore2;
char ch;
int i[4];
file >> ch >> i[0] >> ignore1 >> i[1] >> i[2] >> ignore2 >> i[3];
[Edit: if you're paranoid or really need to verify your input, at this point you can check that ignore1
and ignore2
contain commas.]
In most cases, however, the data are probably related, so you'll want to read an entire line into a single struct (or class):
struct data {
char ch;
int i[4];
std::istream &operator>>(std::istream &is, data &d) {
char ignore1, ignore2;
return is >> ch >> i[0] >> ignore1 >> i[1] >> i[2] >> ignore2 >> i[3];
}
};
Having done this, you can read an entire data
object at a time:
std::ifstream infile("my data file.txt");
data d;
infile >> d;
Or, if you have a whole file full of these, you can read them all into a vector:
std::vector<data> d;
std::copy(std::istream_iterator<data>(infile),
std::istream_iterator<data>(),
std::back_inserter(d));