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

How to print an array while reading a file

本文关键字:打印 数组 文件 读取      更新时间:2023-10-16

我正在读取一个txt文件,其中有25个数字,每行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.

我不明白问题出在哪里。

提前谢谢。

更改行:

    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");
}
int m=5;
int n=5;
int Array[i][j]; // This will raise an error

你可以试试我的以下代码。我已经修复了上面的代码,并将int Array[5][5]移出了while(!inFile.of())的范围。我还移动了

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

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

我测试了修改后的代码,它产生了正确的输出:

1 8 5 7 92 4 6 8 101 1 1 12 2 2 23 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");
}