mercredi 29 mai 2019

Reading In Chars With Whitespace C++

In C++, I am trying to read in input from a file containing a formatted "table" of chars, some of which are spaces. I then need to transfer the characters, including the spaces, into a 2-D char array, with the characters placed in the same position in the new char array as they were in the input file. I am given a width and a height for the input, and then a "table" of chars, with (2 * h) + 1 rows and (2 * w) + 1 columns. I keep having problems with reading in this input into a char array. Can anybody show me how to correctly do this? I have included sample input below. For example, my_chars[0][0] would be '+', and my_chars[1][1] would be ' ', meaning space character.

5 3
+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     |  
+-+ +-+-+-+

Here is one piece of code that I tried, but does not work:

int H = 2 * h + 1;
int W = 2 * w + 1;
for (int i = 0; i < H; i++) {
    string line;
    getline(fin, line);
    for (int j = 0; j < W; j++) {
      my_chars[i][j] = line[j];

Aucun commentaire:

Enregistrer un commentaire