file reading

I'm trying to read a file in a very specific way and am having trouble finding a method I understand.

I want to read each line of a very large text file written like:
X 20 30 40
X 100 0.05 1.021
g ignore this line
additional line I dont care about
probably another
X 1.028 55.03 4e-100
g dont care
Y 10.0215 15246.4 12153
Y...

I want to ignore lines that do not start with "X" or "Y" (the trash lines never start with either)

Once I know I'm at a line I want I will do some decision:
if X: vecx.x = first number, vecx.y = second. vecx.z = last

I've tried using different variations of:


1
2
3
4
5
6
7
8
9
ifstream file;
file.open("file.txt");

string word;
while (file >> word)
{

}
	return 0;


But I can't make the comparisons I need because "word" can be a string, char or double.

Nevermind, as soon as I posted this I thought of a way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string word;
while (file >> word)
	{
		//cout << word << '\n';
		if (word == 'v'){
			file >> x;
			//cout << x << endl;
			file >> y;
			//cout << y << endl;
			file >> z;
			//cout << z << endl;
			cin.get();
		}
	}


I'll toy around with it to make sure that it reads everything correctly. Concerned that the last line might be a problem (never u ed '>>' with text before) but we'll see.

Just finished implementing. Works great.
Last edited on
Topic archived. No new replies allowed.