Reputation: 51
I know this question has been asked before, but I cannot seem to find an answer to my particular problem. I am reading in txt file.
weather.txt
1 52 32
2 54 32
3 54 30
4 48 28
5 37 25
6 37 25
7 46 34
8 55 45
9 59 46
10 61 37
11 55 32
12 59 34
There is more data but for space sake I put this.
I am trying to read into the array 0,0 1,0 2,0 then it will go to 0,1 1,1 2,1 then 0,2 1,2 2,2. Just repeating the rows. With the code I have now it just gets stuck. and the output looks like this....
Location: 0 : 0 Data in textFile: 1 Location: 1 : 0 Data in textFile: 1 Location: 2 : 0 Data in textFile: 1 Location: 0 : 1 Data in textFile: 1 Location: 1 : 1 Data in textFile: 1 Location: 2 : 1 Data in textFile: 1 Location: 0 : 2 Data in textFile: 1 Location: 1 : 2 Data in textFile: 1 Location: 2 : 2 Data in textFile: 1 Location: 0 : 3 Data in textFile: 1 Location: 1 : 3 Data in textFile: 1 Location: 2 : 3 Data in textFile: 1 Location: 0 : 4 Data in textFile: 1 Location: 1 : 4 Data in textFile: 1 Location: 2 : 4 Data in textFile: 1 Location: 0 : 5 Data in textFile: 1 Location: 1 : 5 Data in textFile: 1 Location: 2 : 5 Data in textFile: 1 Location: 0 : 6 Data in textFile: 1 Location: 1 : 6 Data in textFile: 1 Location: 2 : 6 Data in textFile: 1 Location: 0 : 7 Data in textFile: 1 Location: 1 : 7 Data in textFile: 1 Location: 2 : 7 Data in textFile: 1 Location: 0 : 8 Data in textFile: 1 Location: 1 : 8 Data in textFile: 1 Location: 2 : 8 Data in textFile: 1 Location: 0 : 9 Data in textFile: 1 Location: 1 : 9 Data in textFile: 1 Location: 2 : 9 Data in textFile: 1 Location: 0 : 10 Data in textFile: 1 Location: 1 : 10 Data in textFile: 1
It goes through the whole file but restarting back to 0,0.
Location: 0 : 0 Data in textFile: 52
Location: 1 : 0 Data in textFile: 52
Location: 2 : 0 Data in textFile: 52
Any help would be appreciated sorry for all the text just trying to be as clear as possible.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int width = 31;//declaring days or columns for array
int height = 3;//declaring information day and high and low
int data;
/* Code to read in txt file */
ifstream infile;
infile.open("weather.txt");
if (!infile) {
cerr << "Unable to open file C\n";
exit(1); // call system to stop
}
/* end code read text file */
int tempDay[height][width];
//PROBLEM WITH LOOP//
while (infile >> data) {
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height ; ++j) {
tempDay[j][i] = data;
cout << "Location: " << j <<" : " << i << " Data in textFile: " <<data<<endl;
}
}
}
infile.close();
return 0;
}
Upvotes: 0
Views: 5918
Reputation: 5419
The following should work provided the text file is as described and there are any unexpected characters. It is similar to your code except it checks for an unexpected end-of-file (i.e. if there aren't WIDTH
rows in the file) within the for loop.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
const int WIDTH = 31;//declaring days or columns for array
const int HEIGHT = 3;//declaring information day and high and low
/* Code to read in txt file */
ifstream infile;
infile.open("weather.txt");
if (!infile) {
cerr << "Unable to open file\n" << endl;
exit(1); // call system to stop
}
/* end code read text file */
int tempDay[HEIGHT][WIDTH];
for (int i = 0; i < WIDTH; ++i) {
for (int j = 0; j < HEIGHT ; ++j) {
if (!(infile >> tempDay[j][i])) {
cerr << "Unexpected end of file\n" << endl;
exit(1); // call system to stop
}
cout << "Location: " << j <<" : " << i << " Data in textFile: " << tempDay[j][i] << endl;
}
}
infile.close();
return 0;
}
Upvotes: 1
Reputation: 9407
Since you know the sizes, this should work:
const int width = 3;//declaring days or columns for array
const int height = 31;//declaring information day and high and low
// ..
int tempDay[height][width];
// ..
int i = 0;
while (infile >> tempDay[i][0] >> tempDay[i][1] >> tempDay[i][2]) // one line at a time
{
cout << tempDay[i][0] << ' ' << tempDay[i][1] << ' ' << tempDay[i][2] << endl;
i++;
}
Upvotes: 0