Reading Data into an Array

I need to read a large file into arrays but there are different conditions that i need to keep track of. And the file isn't just data there are characters specifying the conditions and i do not need this data in the arrays i just need it to keep track of the conditions. Here is an exert from the file:
VARIABLES=ALPHA,CN,CM,CA,CY,CLN,CLL,DELTA
ZONE T="UNTRIMMED MACH= 0.00"
-28.00 -11.2281 17.4660 1.8896 0.0000 0.0000 0.0000 -25.0000
-24.00 -10.0606 16.4065 1.8716 0.0000 0.0000 0.0000 -25.0000
-20.00 -8.8378 15.4180 1.8338 0.0000 0.0000 0.0000 -25.0000
-16.00 -7.6232 14.5372 1.7842 0.0000 0.0000 0.0000 -25.0000
-12.00 -6.4571 14.0393 1.7517 0.0000 0.0000 0.0000 -25.0000
-8.00 -5.3617 14.0301 1.7495 0.0000 0.0000 0.0000 -25.0000
-4.00 -4.5475 14.9102 1.8112 0.0000 0.0000 0.0000 -25.0000
0.00 -3.9187 16.0172 1.8885 0.0000 0.0000 0.0000 -25.0000
4.00 -2.7982 15.1127 1.7758 0.0000 0.0000 0.0000 -25.0000
8.00 -1.4790 13.9254 1.6440 0.0000 0.0000 0.0000 -25.0000
12.00 0.1944 11.5501 1.4051 0.0000 0.0000 0.0000 -25.0000
16.00 2.0304 8.3113 1.0951 0.0000 0.0000 0.0000 -25.0000
20.00 3.8482 4.9632 0.7815 0.0000 0.0000 0.0000 -25.0000
24.00 5.5551 1.9946 0.4947 0.0000 0.0000 0.0000 -25.0000
28.00 7.1748 -0.9142 0.1943 0.0000 0.0000 0.0000 -25.0000
ZONE T="UNTRIMMED MACH= 0.00"
-28.00 -11.0998 16.9418 1.5245 0.0000 0.0000 0.0000 -20.0000
-24.00 -9.9308 15.8763 1.5228 0.0000 0.0000 0.0000 -20.0000
-20.00 -8.7300 14.9774 1.5072 0.0000 0.0000 0.0000 -20.0000
-16.00 -7.6125 14.4936 1.4991 0.0000 0.0000 0.0000 -20.0000
-12.00 -6.5868 14.5694 1.5142 0.0000 0.0000 0.0000 -20.0000
-8.00 -5.8172 15.8919 1.6027 0.0000 0.0000 0.0000 -20.0000
-4.00 -4.8470 16.1342 1.6088 0.0000 0.0000 0.0000 -20.0000
0.00 -3.6385 14.8719 1.5108 0.0000 0.0000 0.0000 -20.0000
4.00 -2.3128 13.1284 1.3653 0.0000 0.0000 0.0000 -20.0000
8.00 -0.7331 10.8763 1.1884 0.0000 0.0000 0.0000 -20.0000
12.00 1.1795 7.5242 0.9387 0.0000 0.0000 0.0000 -20.0000
16.00 3.0164 4.2819 0.7029 0.0000 0.0000 0.0000 -20.0000
20.00 4.7590 1.2415 0.4770 0.0000 0.0000 0.0000 -20.0000
24.00 6.4696 -1.7417 0.2394 0.0000 0.0000 0.0000 -20.0000
28.00 8.2323 -5.2344 -0.0465 0.0000 0.0000 0.0000 -20.000012

the mach number goes up to 3 with different values for each mach number.
Thanks
Ryan
Hi!

Firstly you should skip the first two line with getline instruction.

"VARIABLES=ALPHA,CN,CM,CA,CY,CLN,CLL,DELTA
ZONE T="UNTRIMMED MACH= 0.00""

Secondly you read the WORDS into the string. Eight words compose a line in the table.
So you should repeat the words reading 8 times (a whole line).

(This code fragment read one word from file. )


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string word;

	fstream textfile;
	textfile.open("text.txt");
	textfile >> word;
	cout << word << endl;
	
	textfile.close();
	return 0;
}



You should repeat textfile >> word; statement 8 times so you read a line. Of course you need to convert the strings to number. (I offer you stringstream to convert http://cplusplus.com/reference/iostream/stringstream/ )
You have to repeat these steps till end of the file.


The other solution is that you use geline (so you can read a line) and convert words from line to number:
http://cplusplus.com/reference/iostream/stringstream/stringstream/

And of course you can repeat these steps till the end of the file.

Other solution
Last edited on
Topic archived. No new replies allowed.