从我的 2DgridArray 中的文本文件中获取每个字符

Getting each character from a textfile in my 2DgridArray

enter image description here 你可以在图片上看到他没有打印出一些奇怪的字母,有时不包括空格。 输出不对,我不知道这些奇怪的字母是从哪里来的..可能是传输不对?

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    const int xarrSize = 15;
    const int yarrSize = 65;

    int x = 0;
    int y = 0;
    char c;
    char grid[xarrSize][yarrSize];
    fstream input;

    input.open("map.txt", fstream::in);
    if (!input)
    {
        cout << "not open";
    }

    while (input >> noskipws >> c)
    {
        grid[x][y] = c;
        y++;
        if (c == '\n')
            x++;
    }

    for (int i = 0; i < xarrSize; ++i)
    {
        for (int j = 0; j < yarrSize; ++j)
        {
            cout << grid[j][i];
        }
        cout << "\n";
    }

    return 0;
}



Is it possible to filtering each character for example my textfile is just some letters like A,B,C

I want that to transform this into my gridarray, to work with the letters to build my game.

But i tried like other ways and searching in youtube and Whosebug and non of these helped me. 


  [1]: https://i.stack.imgur.com/E2HWs.jpg

在下文中,我建议解决以下几点的解决方案:

  1. 当文件无法打开时程序应该终止而不是处理不存在的数据
  2. 应初始化网格。我用了 '。'为此,但任何其他可打印字符都可以
  3. 程序不应超出数组边界。因此 while 循环被关于数组边界的嵌套 for 循环所取代
  4. 制表符在输出中被转换为单个 space 以使其更规则
  5. 输出中未放置换行符以具有一致的数据行
  6. 出于可视化原因,我在顶部添加了一个列计数器(这可能会再次被删除)。添加它只是为了演示目的。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;

const int xarrSize = 15;
const int yarrSize = 65;

int ReadArray(int xsize, int ysize, char grid[xarrSize][yarrSize], istream &input){
    char c;

    for (int i = 0; i < xsize; ++i)
    {
        for (int j = 0; j < ysize; ++j)
        {
            if ( !(input >> noskipws >> c) ){
                return i*ysize+j;
            }
            if (c == '\n') break;
            if (isspace(c)) c = ' ';
            grid[i][j] = c;                
        }
    }
    return xsize*ysize;
}

int main(int argc, char **argv)
{
    char filename[100] = {0};

    char grid[xarrSize][yarrSize];
    fstream input;

    memset(grid, '.', sizeof(grid));
    strcpy(filename, argv[0]);
    strcat(filename, ".cpp");

    input.open(filename, fstream::in);

    if (!input)
    {
        cout << "not open";
        exit(-1);
    }

    int readBytes = ReadArray(xarrSize, yarrSize, grid, input);

    if (readBytes == 0){
        cout << "Could not read file";
        input.close();
        exit(-2);
    }

    for (int i = 0; i < yarrSize; ++i)
    {
        cout << (char)('0' + i % 10);
    }

    cout << "\n\n";

    for (int i = 0; i < xarrSize; ++i)
    {
        for (int j = 0; j < yarrSize; ++j)
        {
            cout << grid[i][j];
        }
        cout << "\n";
    }

    input.close();
    return 0;
}

示例输出:

    01234567890123456789012345678901234567890123456789012345678901234

    0 1 00 01 10 11 000 001   010      011 ..........................
    100 101 110 111 .................................................
    0000 0001 0010 0011 0100 0101 ...................................
    .................................................................
    .................................................................
    0110 0111 1000 1001 1010          1011 1100 1101 1110 ...........
         1111 10011 11001............................................
    .................................................................
    .................................................................
    .................................................................
    .................................................................
    .................................................................
    .................................................................
    .................................................................
    .................................................................