如何在读取文件时打印数组

时间:2014-11-02 20:38:26

标签: c++ arrays file numbers ifstream

我正在读取一个包含25个数字的txt文件,每行5个。

1 8 5 7 9
2 4 6 8 10
1 1 1 1 1
2 2 2 2 2 
3 3 3 3 3 

我的代码是:

#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;

int main()
{
string myFile, mystring;
string DIR;
string extension;
int total = 0;

int number_of_lines = 0;
string line;

extension = ".txt";
DIR = "H:\\Year2\\C++\\EE273\\Week5\\";

cout << "Enter the name of the file ";
cin >> myFile;

myFile = DIR + myFile + extension;
ifstream inFile;

inFile.open(myFile.c_str());

if (!inFile) {
    cout <<"Error opening file"<<myFile<<endl;
    return -1;
}

while (!inFile.eof()){  

        int m=5;
        int n=5;
        int Array[i][j];

        for (int i=0;i<5;i++){
            for (int j=0; j<5; j++){
                inFile >> Array[i][j];
                    for(int row=0;row<5;row++){
                        for (int column=0;column<5;column++){
                        cout<<Array[i][j];
                        }}}}
}

    //cout<<mystring<<endl;
inFile.close();
system("PAUSE");

}

我得到的错误是:

error C2065 for i and j: identifier not declared.

我无法理解问题所在。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

更改行:

    int Array[i][j];

为:

    int Array[5][5];

整个代码(我没有compiller,但也许你想收到这样的东西)

#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;

int main() {
    string myFile, mystring;
    string DIR;
    string extension;
    int total = 0;

    int number_of_lines = 0;
    string line;

    extension = ".txt";
    DIR = "H:\\Year2\\C++\\EE273\\Week5\\";

    cout << "Enter the name of the file ";
    cin >> myFile;

    myFile = DIR + myFile + extension;
    ifstream inFile;

    inFile.open(myFile.c_str());

    if (!inFile) {
        cout <<"Error opening file"<<myFile<<endl;
        return -1;
    }

    while (!inFile.eof()){  
        int m=5;
        int n=5;
        int Array[5][5];

        for (int i=0;i<5;i++){
            for (int j=0; j<5; j++){
                inFile >> Array[i][j];
            }
        }
        for(int row=0;row<5;row++){
            for (int column=0;column<5;column++){
                cout << Array[row][column] << " ";
            }
            cout << endl;
        }
    }
    inFile.close();
    system("PAUSE");
}

答案 1 :(得分:0)

int m=5;
int n=5;
int Array[i][j]; // This will raise an error

您可以尝试以下代码。我修复了上面的代码并将int [5] [5]移出了while的范围(!inFile.eof())。我也搬家了

       for (int row = 0; row<5; row++){
            for (int column = 0; column<5; column++){
                cout << Array[row][column] << " ";
            }
            cout << endl;
        }

超出while的范围(!inFile.eof())。这就是输出重复的原因。

我测试了修改后的代码并生成了正确的输出:

1 8 5 7 9 2 4 6 8 10 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3

#include <iostream>
#include <string>
#include <fstream>
#include <istream>
#include <stdio.h>
using namespace std;

int main() {
    string myFile, mystring;
    string DIR;
    string extension;
    int total = 0;

    int number_of_lines = 0;
    string line;

    extension = ".txt";
     DIR = "H:\\Year2\\C++\\EE273\\Week5\\";

    cout << "Enter the name of the file ";
    cin >> myFile;

    myFile = DIR + myFile + extension;
    ifstream inFile;

    inFile.open(myFile.c_str());

    if (!inFile) {
        cout << "Error opening file" << myFile << endl;
        return -1;
    }

    int Array[5][5];

    while (!inFile.eof()){

        for (int i = 0; i < 5; i++){
            for (int j = 0; j < 5; j++){
                inFile >> Array[i][j];
            }
        }
    }

    for (int row = 0; row<5; row++){
        for (int column = 0; column<5; column++){
            cout << Array[row][column] << " ";
        }
        cout << endl;
    }

    inFile.close();
    system("PAUSE");
}