【问题标题】:How to print an array while reading a file如何在读取文件时打印数组
【发布时间】:2014-12-29 12:16:09
【问题描述】:

我正在读取一个包含 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.

我不明白问题出在哪里。

提前谢谢你。

【问题讨论】:

    标签: c++ arrays file numbers ifstream


    【解决方案1】:

    换行:

        int Array[i][j];
    

    到:

        int Array[5][5];
    

    完整的代码(我没有编译器,但也许你想收到这样的东西)

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

    【讨论】:

    • 现在程序可以运行,但是输出错误。而不是以与 txt 文件相同的格式打印 25 个数字。就像每个数字打印 20 次一样
    【解决方案2】:
    int m=5;
    int n=5;
    int Array[i][j]; // This will raise an error
    

    你可以试试我下面的代码。我已修复上述代码并将 int Array[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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-22
      • 2021-09-16
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多