c++ - 访问冲突循环和写入

标签 c++ for-loop access-violation writing

好的,我已经检查了有关此问题的每个问题,但仍然不知道。我需要使用循环将字符串数组写入文件。我的阅读部分运行正常,但是在调试时这不是特定的。所以,这就是问题所在。

它给了我:访问冲突,但是不能给我确切的界线。
我对C++还是很陌生,而且我不太擅长异常处理。 编辑:
变量和数组在上半部分声明,但我将给出它们。

string bord[10][10];
string line;
int i=0,j=1;
ifstream ifile ("test.txt");
//readfile
if (ifile.is_open()){
    getline (ifile,line);
    bord[0][0].assign(line,0,1);
    cout << "i=" << i << " j=" << j<< " inhoud - " << bord[0][0] << endl;
    while (j!=11){
        getline (ifile,line);
        cout << line << endl;
        i=0;
        while (i!=10){
            bord[i][j].assign(line,i,1);
            cout << "i=" << i << " j=" << j<< " inhoud - " << bord[i][j] << endl;
            i++;
        }
        j++;
    }
    ifile.close();
}
ofstream ofile ("test2.txt", ios_base::trunc);
                    //Write File
if (ofile.is_open()){
    if(bord[0][0]=="z"){
        ofile << "zwart";
    }
    else{
        ofile << "wit";
    }
                    //START LOOPING FOR OUTPUT
    for(j=1; j<11; j++){
        ofile << endl;
        for(i=0; i<10; i++){
            cout <<  "i=" << i << " j=" << j<< " inhoud - " << bord[i][j] << endl;
            ofile << bord[i][j];
        }
    }
    ofile.close();
}

最佳答案

bord上的尺寸不正确。根据您自己的代码,它应该是string bord[10][11];而不是string bord[10][10];
问题从while (j!=11){'. Note how j开始到此循环最多10个。这意味着您的内部尺寸至少应为11。

关于c++ - 访问冲突循环和写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6213878/

相关文章:

c# - 在 ASP.Net 应用程序中调用非托管代码时获取 protected 内存异常

C++ OpenCV mat.at 在使用数据时出现访问冲突

compiler-errors - 德尔福 XE 2010 : Access Violation - How to remove package or component?

c++ - 对于 C++ 中的图问题,邻接列表或邻接矩阵哪个更好?

c++ - rapidjson + c++ : "abort() has been called" error

javascript - 在 Visual Studio 中调试 javaScript。第一次机会异常(exception)

javascript - 为什么这个 for 循环不起作用?

c++ - Xcopy文件到pro/pri文件中的新目录

php - 如何在循环中创建换行符

c - C中的for循环条件