-
Input stream format
I need to write some code that will read in some numbers in various formats. The problem is that there are a few different ways the numbers can be arranged.
n1 n2 n3
or
n1/n2 n3/n4 n5/n6
or
n1/n2/n3 n4/n5/n6 n7/n8/n9
This is relatively easy to solve with scanf but as my project is in C++ and I'm using streams everywhere else, I would like to solve this using streams as well.
I would appreciate some help on how to solve this problem. Thanks.
P.S. In case anybody is curious the above is a graphics format. We can either have 3 vertices alone, 3 vertices along with their corresponding texture coordinates, or 3 vertices along with their texture coordinates and normals.
-
>>n1/n2 n3/n4 n5/n6
Code:
ifstream file("test.txt");
vector<pair<double,double> > values;
typedef vector<pair<double,double> >::const_iterator citer;
double val1 = 0,val2 = 0;
char c = 0;
while(!file.fail())
{
file >> val1 >> c;
if(file.fail() || c != '/')
break;
file >> val2;
values.push_back(make_pair(val1,val2));
}
for(citer i = values.begin();i != values.end();++i)
cout << i->first << '/' << i->second << '\n';