将txt文件读入2d数组C ++

时间:2017-04-21 03:30:13

标签: c++

我知道之前已经问过这个问题,但我似乎无法找到我特定问题的答案。我正在读取txt文件。

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

有更多的数据,但为了太空,我把它。

我试图读入数组0,0 1,0 2,0然后它将转到0,1 1,1 2,1然后0,2 1,2 2,2。只是重复行。有了我现在的代码,它就会卡住。 输出看起来像这样......

位置:0:0 textFile中的数据:1 位置:1:0 textFile中的数据:1 位置:2:0 textFile中的数据:1 位置:0:1 textFile中的数据:1 位置:1:1 textFile中的数据:1 位置:2:1 textFile中的数据:1 位置:0:2 textFile中的数据:1 位置:1:2 textFile中的数据:1 位置:2:2 text中的数据文件:1 位置:0:3 textFile中的数据:1 位置:1:3 textFile中的数据:1 位置:2:3 textFile中的数据:1 位置:0:4 textFile中的数据:1 位置:1:4 textFile中的数据:1 位置:2:4 textFile中的数据:1 位置:0:5 textFile中的数据:1 位置:1:5 textFile中的数据:1 位置:2:5 textFile中的数据:1 位置:0:6 textFile中的数据:1 位置:1:6 text中的数据文件:1 位置:2:6 text中的数据文件:1 位置:0:7 textFile中的数据:1 位置:1:7 textFile中的数据:1 位置:2:7 text中的数据文件:1 位置:0:8 textFile中的数据:1 位置:1:8 textFile中的数据:1 位置:2:8 textFile中的数据:1 位置:0:9 textFile中的数据:1 位置:1:9 textFile中的数据:1 位置:2:9 text中的数据文件:1 位置:0:10 textFile中的数据:1 位置:1:10 textFile中的数据:1

它遍历整个文件但重新开始回到0,0。

位置:0:0 textFile中的数据:52

位置:1:0 textFile中的数据:52

位置:2:0 textFile中的数据:52

任何帮助都会感谢抱歉所有文本只是尽可能清楚。

#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;     
}

2 个答案:

答案 0 :(得分:0)

既然您知道尺寸,这应该有效:

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++;
}

答案 1 :(得分:0)

如果文本文件与描述相同并且存在任何意外字符,则以下内容应该有效。它类似于您的代码,除了它在for循环中检查意外的文件结尾(即文件中是否有WIDTH行)。

#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;     
}
相关问题