conversion of .txt into array

Hello! i need some help! im trying to figure out how to take a .txt file an put it into a char array so i can use it for my game. I dont understand what im doing wrong. can someone please help me.

heres my code:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

char Map[16][49];

int main () {
  string line;
  ifstream myfile ("Display.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
        for(int a = 0; a != 16; a++)
        {
            for(int b = 0; b != 49; b++)
            {
                Map[a][b] = line[b];
            }
            cout << '\n';
        }
    }
    myfile.close();
  }

  else cout << "Unable to open file";

  for(int a = 0; a != 16; a++)
  {
      for(int b = 0; b != 49; b++)
      {
          cout << Map[a][b];
      }
      cout << '\n';
  }
  return 0;
}


heres my "Display.txt"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
|                                                      |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Level: ??
 Save: ??


and this is what is output onto the consol






 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||
 Save: ?? |||||||||||||||||||||||||||||||||||||||

Process returned 0 (0x0)   execution time : 0.199 s
Press any key to continue.


what am i doing wrong? any help would be greatly appreciated! thanks in advance.
Change your reading to:
1
2
3
4
5
6
7
8
9
10
11
12
  if (myfile.is_open())
  {
    for (int a = 0; a != 16 && getline (myfile,line); a++ )
    {
        for(int b = 0; b != 49; b++)
        {
            Map[a][b] = line[b];
        }
        cout << '\n';
    }
    myfile.close();
  }

You were counting a from 0 for each line. That means you were writing the same thing to each line. You only want to increment a when you get a new line.
Last edited on
thx, that helped it somewhat but theres still a bunch of spaces at the beginning. how do i fix that?
I would just do something like:
1
2
3
4
5
6
7
8
9
int row = 0;
while(std::getline(myfile, line))
{
    //iterate over string and assign or use a stringstream
    std::stringstream ss(line);
    int column = 0;
    while(ss >> Map[row][column++]);
    ++row;
}


*edit forgot the column variable

PS you shouldn't use magic numbers like 16 and 49. You should assign them to a constant variable like rows and columns. Then you can use for loops and what ever else needs the rows/columns very easy and you can change the rows/columns without having to modify all the 16s/49s.
Last edited on
oh and i resized my "Display.txt" to fit the 49 X 16 that i specified so that its all on there now.
i know. i only used numbers here to try an figure it out. once i get it figured out completely im going to use it in my game. its an ASCII scroller in the consol. next step after this is scrolling
giblet thanks a lot. that fixed all my issues. and it gave me an idea for scrolling.
Topic archived. No new replies allowed.