【问题标题】:How to read a matrix from text file and store it into 2D array?如何从文本文件中读取矩阵并将其存储到二维数组中?
【发布时间】:2015-10-29 13:22:49
【问题描述】:

我是这方面的初学者。我正在尝试读取文件并将其放入二维数组中。这是我的代码。在它输出文件后,它会显示内存中的垃圾,并且循环永远不会结束,除非它达到 50。

include "stdafx.h"
#include <iostream> 
#include <fstream>  
using namespace std;

void main()
{
    char arr[50][50];
    ifstream fin;
    fin.open("Map.txt");

    for (int i = 0; i < 50; i++)
    {

        for ( j = 0; j < 50; j++)
        {
            fin.get(arr[i][j]);
        }


    }
    for (int i = 0; arr[i]!=NULL; i++)
    {
        for (int j = 0; arr[j]!=NULL; j++)
        {
            cout<< arr[i][j];
        }
    }



}

文本文件如下所示

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@                                        @@
@@                                        @@ 
@@                                        @@
@@                 ^                      @@ 
@@                                        @@
@@                                        @@
@@                                        @@  
@@@@@@@@@@@@@@@@                          @@
              @@                          @@
@@@@@@@@@@@@@@@@                          @@
@@                                        @@
@@  x x                                   @@
@@                                        @@
@@                                  o     @@
@@                                        @@
@@                        o               @@
@@                                        @@ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

【问题讨论】:

  • Map.txt里面的数据是怎么排列的?
  • 您是要阅读 50 个单词还是 2500 个字符?
  • 您的代码无法编译:j 未定义
  • 输出循环的结束条件是什么?内部循环中的arr[j] 似乎是错误的,为什么您不能编写与输入循环中相同的条件?
  • 我正在尝试读取少于 50 个字符。

标签: c++ arrays file text multidimensional-array


【解决方案1】:

我认为这样的方法可行

#include <iostream>
#include <fstream>

int main() {

const int nSize = 50;
//-- initialize array with 0 --
char map[nSize][nSize] = { {0} };

std::ifstream in;
in.open("input.txt");

int i = 0, j = 0;
while (!in.eof()) {
    char next = in.get();
    if (next != '\n')
        map[i][j++] = next;
    else {
        j = 0;
        i++;
    }
}

int rowsCount = i + 1;
for (i = 0; i < rowsCount; i++) {
    j = 0;
    while (map[i][j] != 0) 
        std::cout << map[i][j++];
    std::cout << std::endl;
}


return 0;
}

所有文本行都以“结束行符号”“\n”或“\r\n”结尾。这可以表示转到数组中的新字符行。 由于数组初始化为 0,我们可以将其用作输出中行尾的标志,但最好在读取数组时计算数组的大小(如果所有行的大小相同)。

【讨论】:

    【解决方案2】:

    试试这个,但要确保Map.txt中的输入矩阵一定是50*50个字符,否则你可能会收到不确定的结果。 (include "stdafx.h" 丢失了,因为我使用 g++ 而不是 MS Visual Studio,但是如果您按照 VS 需要的方式创建项目,您可以为预编译的头文件添加此包含)

    #include <iostream> 
    #include <fstream>
    #include <string>
    using namespace std;
    
    const unsigned int HEIGHT = 50;
    const unsigned int WIDTH = 50;
    
    int main()
    {
        char arr[HEIGHT][WIDTH];
    
        ifstream fin;
        fin.open("Map.txt");
        string line;
    
        //let's assume here the proper size of input Map
        for(unsigned int i = 0; i < HEIGHT; i++)
        {
          getline(fin, line);
          for(unsigned int j = 0; j < WIDTH; j++)
          {
            arr[i][j] = line[j];
          }
        }
    
        //let's assume here the proper size of input Map
        for (int i = 0; i < HEIGHT; i++)
        {
            for ( int j = 0; j < WIDTH; j++)
            {
                cout << (char)arr[i][j];
            }
            cout << endl;
        }
    }
    

    【讨论】:

    • @Semyon Burov:Ypur 解决方案比我建议的要好得多。我认为 haitham hany 会满足于更简单但不太安全的解决方案的原因,以便首先了解有关此问题的更多信息。无论如何,干得好!
    【解决方案3】:

    如果你想做我认为的那样,试试这个:

    include "stdafx.h"
    include <iostream> 
    include <fstream>  
    using namespace std;
    
    
    
       void main()
    {
        char arr[50][50];
        ifstream fin;
        fin.open("Map.txt");
    
        for (int i = 0; i < 50; i++)
        {
    
            for ( int j = 0; j < 50; j++)
            {
                fin.get(arr[i][j]);
            }
    
    
        }
        for (int i = 0; arr[i]!=NULL; i++)
        {
            for (int j = 0; arr[j]!=NULL; j++)
            {
                cout<< arr[i][j];
            }
        }
    
    
    
    }
    

    文本文件中的数据是:

    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    @@                                        @@
    @@                                        @@ 
    @@                                        @@
    @@                 ^                      @@ 
    @@                                        @@
    @@                                        @@
    @@                                        @@  
    @@@@@@@@@@@@@@@@                          @@
                  @@                          @@
    @@@@@@@@@@@@@@@@                          @@
    @@                                        @@
    @@  x x                                   @@
    @@                                        @@
    @@                                  o     @@
    @@                                        @@
    @@                        o               @@
    @@                                        @@ 
    @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    

    【讨论】:

    • 这对填充二维数组有何帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2015-04-16
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多