0

I have a file Map.txt and there is a 2D array saved inside of that file, but whenever I try to print my 2D array in my main program I get crazy numbers. Code:

  cout << "Would you like to load an existing game? Enter Y or N: " << endl;
cin >> Choice;
if (Choice == 'Y' || Choice == 'y')
{
   fstream infile;
   infile.open("Map.txt");
   if (!infile)
       cout << "File open failure!" << endl;
   infile.close();
}
if (Choice == 'N' || Choice == 'n')
    InitMap(Map);

Map saved in file:

********************
********************
********************
********************
********************
********************
********************
**********S*********
*****************T**
********************

Output when program is run:

Would you like to load an existing game? Enter Y or N: 
y
88???????`Ė
?(?a????
??_?
?дa??g  @
 Z???@

        ?
 ?a??p`Ė??p]?
??_???`Ė?
??a??#E@??
??_??
10
  • 3
    Please show exactly what the file looks like, as well as the relevant part of the code which actually attempts to print. Commented Apr 8, 2014 at 3:51
  • You have shown the code of opening and closing the file. Put the code of reading the 2D array from the file also and what kind of output you are getting. Commented Apr 8, 2014 at 3:54
  • I don't know how to read the 2D array from the file. Commented Apr 8, 2014 at 3:55
  • 1
    How could you possibly expect the output not to be crazy then? Commented Apr 8, 2014 at 3:59
  • 2
    Never mind a 2D array, do you know how to read one character from a file? Have you tried looking up "file" in a C++ textbook? Or an online search? Have you tried anything? Commented Apr 8, 2014 at 4:03

2 Answers 2

1

I am going to hazard a guess that you want to read the file into a 2D character array. I will also assume for simplicity that you know how many rows and columns you need. The numbers below are for illustration only.

#define NUM_ROWS 10
#define NUM_COLS 20    

// First initialize the memory
char** LoadedMap = new char*[NUM_ROWS];
for (int i = 0; i < NUM_ROW; i++)
   LoadedMap[i] = new char[NUM_COLS];

// Then read one line at a time
string buf;
for (int i = 0; i < NUM_ROW; i++) {
   getline(infile, buf);
   memcpy(LoadedMap[i], buf.c_str(), NUM_COL);
}

// Sometime later, you should free the memory


for (int i = 0; i < NUM_ROW; i++)
   delete LoadedMap[i];

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

Comments

0

This Code will show your Map.txt file in console. Don't forget to provide the exact path to open the file.

#include <stdio.h>

const int MAX_BUF = 100001;
char buf[MAX_BUF];

int main()
{
    FILE *fp = fopen("Map.txt","r"); //give the full file path here.
    while( fgets(buf,MAX_BUF,fp) )
    {
        puts(buf);
    }
    return 0;
}

Comments

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.