简体   繁体   English

多维数组分割错误

[英]Segmentation fault with multidimensional arrays

I tried searching through this website for possible answers of why this error is occurring but couldn't find the exact answer. 我尝试通过该网站搜索有关为什么发生此错误的可能答案,但找不到确切答案。

For this little code, I basically have the program read input from a file. 对于这个小代码,我基本上让程序从文件中读取输入。 (It reads every single character.) Then store it in a multi-dimensional array (2D) and finally print out the array. (它读取每个字符。)然后将其存储在多维数组(2D)中,最后打印出该数组。

This is my code: 这是我的代码:

ifstream file;
char gamemap[20][26];
file.open("maze-hard.txt");
if(!file.is_open())
{
    cout << "Error: Cannot open file" << endl;
    return 0;
}

    for(int i = 0; i < 20; i++) 
    {
        for(int j = 0; j < 26; i++)
        {
            gamemap[i][j] = file.get();
            cout << gamemap[i][j];

        }
        cout << endl;
    }

It was somewhat successful, but I got a segmentation fault error. 它取得了一些成功,但是出现了分段错误错误。 I don't know where the problem lies. 我不知道问题出在哪里。 Don't go hard on me for this one. 不要为此而努力。 I'm not all that advanced in C++. 我对C ++的了解还不够。

 for(int j = 0; j < 26; i++)

我会用j++代替i++ j++

在第二个循环中递增j,而不是i;)

While probably not strictly related to this particular segfault, I'd also check to make sure that the read is good for safety. 尽管可能与该特定段不严格相关,但我还要检查以确保读取对安全性有益。

if(file.good())
{
    gamemap[i][j] = file.get();
    //etc etc
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM