Reading in from file

I have some data from an experiment that I want to analyse the format is :

theta w(theta) err w(t) ( this part is not in the text file only numbers)

17.500000 1.101942 0.023009
37.500000 1.030780 0.017027
55.000000 0.951339 0.013554
71.000000 0.993998 0.015127
90.000000 0.931244 0.010707
109.000000 0.942470 0.014515
125.000000 0.953977 0.013580
142.500000 1.009818 0.016766
162.500000 1.027957 0.022473
180.000000 1.056475 0.077017


after I get the arrays built I will do some fancy math and print out an answer. I am having trouble getting them into an array in the first place. I am not sure how to go about doing the loops to pull a certain column only. so far i can read a file that only looks like :

x vales

1
22
123
2
34
ect...

the file can only have one column in it. the file that im working with has more therefore this code will not work...

Any help would be great!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>	
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

//======================================================================


int main () 
{
	start:
	cout << "Input filename where the data exist-->";
	char filename [50];
	ifstream dat;
	cin.getline(filename,50);
	dat.open(filename);


	if (!dat.is_open()){
		cout << "File Does Not Exist!" << endl ;
	goto start;
	}

	
	long theta[100], wthe[100], errwth[100];	
	int x = 0; 
	while (! dat.eof()){
		dat >> theta[x];

		cout << theta[x] << endl;

		x++;
	}



return 0 ;
}
Last edited on
Your numbers are not long. However, this might be an occasion, when while ( dat >> a >> b >> c ) could be appropriate. Then again, static arrays are not as fun as std::vector and a POD struct.
so I should use float instead of long ?
I got it working here is the program that works for reference:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>	
#include<cmath>
#include<string>
#include<fstream>

using namespace std;

//======================================================================


int main () 
{
	start:
	cout << "Input filename where the data exist-->";
	char filename [50];
	ifstream dat;
	cin.getline(filename,50);
	dat.open(filename);


	if (!dat.is_open()){
		cout << "File Does Not Exist!" << endl ;
	goto start;
	}

	
	float theta[100], wthe[100], errwth[100];	
	int x = 0; 



	double a,b,c;

	while (! dat.eof()){

		dat >>  a >>  b >>  c;

		theta [x] = a ;
		wthe [x] = b;
		errwth [x] = c;
	
		cout << wthe [x] << endl;
		x++;
	}


return 0 ;
}
			
Topic archived. No new replies allowed.