简体   繁体   English

如何从文本文件中读取矩阵并将其存储到2D数组中?

[英]How to read a matrix from text file and store it into 2D array?

I am a beginner at this. 我是初学者。 I'm trying to read a file and put it into a 2D array. 我正在尝试读取文件并将其放入2D数组。 Here is my code. 这是我的代码。 after it outputs the file it displays the garbage in the memory and the loop never ends unless it hits 50. 输出文件后,它会在内存中显示垃圾,除非循环达到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];
        }
    }



}

The text file looks like this 文本文件如下所示

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

I think something like this works 我认为像这样的作品

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

All lines of text ends with "end line symbol" '\\n' or '\\r\\n'. 文本的所有行均以“结束行符号”“ \\ n”或“ \\ r \\ n”结尾。 This can signalize to go to new row of chars in array. 这可以表示要转到数组中的新字符行。 Since array was initialized with 0, we can use it as flag of end of row in output, but better would be calculate size of array while reading it (if all lines have same size). 由于数组已初始化为0,因此我们可以将其用作输出中行尾的标志,但最好是在读取数组时计算数组的大小(如果所有行的大小都相同)。

Try this, but make sure the input matrix in Map.txt is 50*50 characters for sure, otherwise you may receive undetermined results. 尝试执行此操作,但是请确保Map.txt中的输入矩阵确定为50 * 50个字符,否则可能会收到不确定的结果。 ( include "stdafx.h" is missing, because I use g++ instead of MS Visual Studio, but you may add this include for precompiled headers, if you created your project the way VS needs it) (因为我使用g ++而不是MS Visual Studio,所以缺少了include "stdafx.h" ,但是如果您以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;
    }
}

If you are trying to do what I think you are, try this: 如果您要尝试做我认为的事情,请尝试以下操作:

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];
        }
    }



}

the data inside the text file is : 文本文件中的数据为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件中读取“不均匀”矩阵,并存储到2D数组中? - How to read an “uneven” matrix from a file, and store into a 2D array? 如何读取文件并将其存储为矩阵(二维数组)? - How do I read a file and store it as a Matrix (2D aray)? 如何从文件中读取并将内容存储在动态二维数组中并以网格格式显示? - How do I read from a file and store the content in a dynamic 2d array and display it in a grid format? 读取文本文件中的C ++ 2D数组 - C++ 2D array from read text file 如何将文本文件的未知内容读取到2D数组中 - How to read an unknown contents of a text file into a 2d array 如何将 csv 文本文件读入二维数组? - How do i read a csv text file into a 2D array? 如何标记文件中的值并将其存储在2D数组中 - How to tokenize values from a file and store it in a 2D array 读取包含二维矩阵和相关数据的文本文件 - Read text file containing 2D matrix and associated data C++ 如何将数字存储在文本文件中,其中字符、数字和逗号的内容以二维矩阵分隔 - C++ How to store numbers in a text file with contents of characters , numbers , and comma delimited in a 2D matrix 如何从txt文件中读取迷宫并将其放入2D数组中 - How to read a labyrinth from a txt file and put it into 2D array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM