从char文件读入2d数组(不知道大小) - c ++

时间:2018-04-01 13:37:14

标签: c++ file multidimensional-array

我收到了一个包含此测试用例字符的文件:

+++X..X---
++++XX----

当我尝试从文件读入2d数组时,如下所示,每次运行代码时结果都会改变。有时它会正确读取/打印,而有些它只打印一些字符,而不会丢失任何错误。我怎么能解决这个问题?

  int main() {
        ifstream myFile;
        myFile.open("map");
        int N = 1000; //max size
        int M = 1000;
        char map_universe[N][M];

        noskipws(myFile);
        char a;
        int i = 0;
        int j = 0;
        while (!myFile.eof()) {
            myFile >> a;
            if (a != '\r' && !myFile.eof()) {
                map_universe[i][j] = a;
                cout << map_universe[i][j];
                j++;   
             } else {
                i++;
                j = 0;
            }
        }
        myFile.close();

0 个答案:

没有答案