Arrays, File Access, and other things

Hey everyone. Im working on a project using temperature sensors. the sensors automatically store the temperature in a file as the 21st element in that file in the form t=(temp).

I am trying to read the file into an array and access the 21st element of that array. then write that back to a seperate file. However, am unable to open the file according to my code as it returns the else statement.

Can anyone help me out?

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
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <sstream>
#include <fstream>


using namespace std;


int main()
{

	int  i,j;
	string Data[22];
	double TempsinC[10];
	string temperature;
	int tempC;
	double tempF[10];
	
	ifstream temp1;
	temp1.open("/sys/bus/w1/devices/28-000004dd7c78/w1_slave.txt");
	if(temp1.is_open())
	{	
		for(i = 0; i < 22; i++){
			temp1 >> Data[i];	
			}

		temperature = Data[21];
		temperature.erase (0,2);
		istringstream(temperature) >> tempC;
		tempF[0] = tempC/1000;
	}
	
	ifstream temp2;
	temp2.open("/sys/bus/w1/devices/28-000004de8057/w1_slave.txt");
	if(temp2.is_open())
	{	
		for(i = 0; i < 22; i++){
			temp2 >> Data[i];	
			}

		temperature = Data[21];
		temperature.erase (0,2);
		istringstream(temperature) >> tempC;
		tempF[1] = tempC/1000;
	}
	
	
	else cout << "could not open file";
	
	ofstream Array;
	Array.open ("/home/pi/Desktop/temperatureArray.txt");
	
	
		for(j = 0;j < 10; j++){
			TempsinC[j] = tempF[j];
			Array << TempsinC[j] << endl;			
		}
	
	Array.close();	

	return 0;
}


Topic archived. No new replies allowed.