I'm having trouble trying to load data from a file.
I have an array that I'm saving to a file using this function:
Code:
void option_6() { //Save To File
cout << "\nEnter the name you'd like to save your file as: ";
cin >> FileName;
outFile.open(FileName, ios::out);
outFile << station.size() << "\n";
for(int i = 0; i < station.size(); i++) {
outFile << station[i].StationDesignation << "\n" << station[i].TemperatureF << "\n" << station[i].TemperatureC << "\n" << station[i].DailyCount << "\n";
}
outFile << Average << "\n" << AverageC;
outFile.close();
cout << "File saved as " << FileName << endl;
}
The outputted file is:
Code:
2
Fremont
55
12.7778
5.5
Tustin
55
12.7778
5.5
55
12.7778
The first line is the size of the vector (stationSize)
lines 2-5 and 6-9 are elements of the vector (station[i].StationDesignation, station[i].TemperatureF, station[i].TemperatureC, and station[i].DailyCount)
and 10-11 are two more variables (Average and AverageC).
*The data was generated from previous parts of the program.*
I'm trying to read the data from the file back into those same variables using the following function, but I'm not sure exactly what I'm doing wrong.
Code:
void option_7() { //Read From File
int stationSize;
cout << "Enter the name of the file you want to open: ";
cin >> FileName;
outFile.open(FileName, ios::in);
outFile >> stationSize;
station[stationSize];
for(int i = 0; i < station.size(); i++) {
outFile >> station[i].StationDesignation >> station[i].TemperatureF >> station[i].TemperatureC >> station[i].DailyCount;
}
outFile >> Average >> AverageC;
outFile.close();
cout << "Opened " << FileName << "." << endl;
}
I've attached the code for the whole program, if it'll help. Although I must warn, it's pretty messy.