You need to process each of the point's data lines individually and store them into some type of data structure so that they can be processed after X, Y and Z have been loaded. You should also include some additional validation code otherwise you are likely to introduce undefined behavior. There are several ways to do this, some will cause you to duplicate code others concentrate on assigning the data after it has been processed.
The solution below takes into account both validation and the fact that the data must be read from 3 individual lines. It's not perfect and expects the "myfile.txt" file to be in an exact format. It uses exceptions to handle errors in the data format and adds a data structure for saving the data while it's being loaded.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <sstream>
#include <stdexcept>
using namespace std;
int main()
{
const char FileName[] = "myfile.txt";
string line;
ifstream file;
file.open (FileName);
for(;;)
{
getline(file, line);
if( file.eof() ) {
break;
}
if (line.size() && line[0]=='P')
{
// Change "int" to "double" if necessary.
struct { int x, y, z; } data;
for(int i = 0; i < 3; i++)
{
getline(file, line);
if(line.size() > 3 && line[0] == '<' && line[2] == '>')
{
string::value_type pointElement = line[1];
// skip spaces and process data here to get the value
string::size_type offset = line.find(' ');
if(string::npos == offset)
{
throw invalid_argument("Invalid data format");
}
stringstream sline(line.substr(offset));
int value;
if(!(sline >> value))
{
throw invalid_argument("invalid data format");
}
switch(pointElement)
{
case 'X': data.x = value; break;
case 'Y': data.y = value; break;
case 'Z': data.z = value; break;
default:
// error in data format
throw invalid_argument("invalid data format");
}
}
else
{
// error in data format
throw invalid_argument("invalid data format");
}
}
// Do something with the values in data
cout
<< "point[x=" << data.x
<< ", y=" << data.y
<< ", z=" << data.z
<< "]" << endl;
}
}
file.close();
return 0;
}
When run against "myfile.txt" it produces the following output
point[x=2, y=5, z=6]
point[x=4, y=2, z=0]
point[x=4, y=1, z=2]