0

私はC ++でファイルの読み取りを行っていますが、これは私のコードです:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <climits>
using namespace std;

int main()
{
int row=0;
int col=0;
ifstream inputFile;
int arr[16][5];

    inputFile.open("hdtt4req.txt");

if(inputFile.is_open()) {
        inputFile >> arr[row][col];
        for (row = 0; row < 16; row++) {
            for (col = 0; col < 5; col++) {
                cout <<"hi"; //arr[row][col];
                cout << endl;
            }
        }
    }
    return 0;
}

そして、これは私が読みたいファイルです:

1 2 2 1 2 
2 1 1 1 2 
3 1 1 1 6 
4 2 2 3 2 
1 2 5 1 2 
2 0 4 3 2 
3 1 2 1 0 
4 2 2 1 2 
1 2 1 1 2 
2 0 0 5 1 
3 2 1 4 1 
4 6 1 2 1 
1 3 1 2 1 
2 1 4 1 4 
3 3 3 2 1 
4 2 0 1 1 

コンパイルした後、このような結果が得られます。誰がエラーが何であるか教えてもらえますか? ありがとう

4

1 に答える 1

5

行と列は未定義で始まるため、このステートメントinputFile >> arr[row][col];は未定義の動作をもたらします。操作を実行する前に、これらの値をゼロに設定してください

row = col = 0;
于 2012-12-29T16:54:01.583 に答える