jeudi 29 octobre 2015

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



}

Aucun commentaire:

Enregistrer un commentaire