Reading from file and storing to 2D array

I want to make a program that reads a map from a txt file and stores it in a two dimensional array of chars. The map is made of " * " and the size of it is 20x25. So this is what i came up with for which is important to mention that it works fine on Dev C++ but not on netbeans 7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>


using namespace std;

class Map{
      private:
              char map[20][25]; //array of map to be stored
      public:
             Map(char *x){
                      char buffer[26];
                      int counter=0;
                      
                      ifstream map_p(x);
                      
                      if (!map_p.is_open())
                       { cout << "Error opening file"; exit (1); }
                       
                      for(int i=0; i<25; i++){
                                          map_p.getline(buffer,26);
                                          strcpy(*(map+counter),buffer);
                                          counter++;
                                          }
                                                                
                  }
          
      void printMap(){
           for(int i=0; i<20; i++){
                   for(int j=0; j<25; j++){
                           cout<<map[i][j];
                           }
                           cout<<endl;
                           }
                           }
      };


int main(int argc, char *argv[])
{
    char *y;
    strcpy(y,"map3.txt");
    Map x(y);
    x.printMap();
    system("PAUSE");
    return EXIT_SUCCESS;
}


I feel that something is wrong with the pointers because if i use strings instead of chars the map is printed fine on netbeans but i can't store it in the array.
Last edited on
Line 23 is wrong. The value should be 20.

You don't need count.

Line 25: strcpy stores the trailing 0 so you need strncpy http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

Line 25 should lool like this: strncpy(map[i],buffer,25);


[...] if i use strings instead of chars the map is printed fine on netbeans but i can't store it in the array.
Why not?
I apologize i didn't elaborate enough. I want to have instant access on any part of the array to insert new characters and strings don't work for me. I found a solution and i don't know if it's the best one but it seems to work fine. Thanks for pointing out the mistake on line 23.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

#include <iostream>
#include <fstream>


class Map {

    char map[20][25];

public:

    Map(char *x) {

        std::ifstream map_p(x); 


        if (!map_p.is_open()) {

            //throw exception to notify main

            throw std::invalid_argument("file could not be opened");

        }

        else {

            char buffer[27];
            int i = 0;

            while(i < 20) { 

                map_p.getline(buffer,27);

                for (int j=0;j<25;j++)

                    map[i][j]=buffer[j];
                i++;

            }

            map_p.close();

        }

 

    }

    void printMap() {

        for(int i=0; i<20; i++) {

            for(int j=0; j<25; j++) {

            std::cout<<map[i][j];

        }

            std::cout<<std::endl;

     }

    }

};


What i still don't get is why the buffer size needs to be 27 or more for it to work properly (i found out about that by chance).
Topic archived. No new replies allowed.