0

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               @@
@@                                        @@ 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5
  • 3
    How is the data arranged inside Map.txt? Commented Oct 29, 2015 at 13:28
  • 1
    Are you trying to read 50 words or 2500 individual characters? Commented Oct 29, 2015 at 13:29
  • your code cannot be compiled: j is not defined Commented Oct 29, 2015 at 13:30
  • What's with end conditions of output loops? arr[j] in inner loop seems wrong and why you can't write same conditions as in input loops? Commented Oct 29, 2015 at 13:51
  • I am trying to read less than 50 characters. Commented Oct 29, 2015 at 13:56

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

@Semyon Burov: Ypur solution is much nicer than the one I suggested. The reason why I think haitham hany would be satisfied with a simpler, but not so secure solution too to learn more about this problem first. Anyway, nice job!
-1

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

1 Comment

How does this help in populating a 2D array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.