haitham hany
haitham hany

Reputation: 33

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. 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.

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               @@
@@                                        @@ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Upvotes: 0

Views: 3363

Answers (3)

vacuumhead
vacuumhead

Reputation: 509

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               @@
@@                                        @@ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Upvotes: -1

Janos
Janos

Reputation: 720

Try this, but make sure the input matrix in Map.txt is 50*50 characters for sure, otherwise you may receive undetermined results. (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)

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

Upvotes: 0

Semyon Burov
Semyon Burov

Reputation: 1172

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'. 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).

Upvotes: 2

Related Questions