尝试关闭ofstream文件后崩溃

时间:2017-11-02 21:20:59

标签: c++ visual-studio fstream

在这段代码中,我打开4个fieles进行写入,在for周期后关闭它们,但编译器在第一个文件(outLess5.close();)的第一次关闭时崩溃。假设你知道所有变量。 这个错误突然出现了。我甚至不明白在哪里。

    std::ofstream outLess5("5.txt");
    std::ofstream outLess7("7.txt");
    std::ofstream outFrom2To3("2_3.txt");
    std::ofstream outFrom5To7("5_7.txt");

    for (int i = 0; i < counter; i++)
    {
        double R = sqrt(pow((Xcentral - X[i]), 2) + pow((Ycentral - Y[i]), 2) + pow((Zcentral - Z[i]), 2));
        if (R < latticeParameter * (parameter / 2 - 2))
        {
            MyPrettyMassive[0][0][newCounter0] = i;
            MyPrettyMassive[0][1][newCounter0] = X[i];
            MyPrettyMassive[0][2][newCounter0] = Y[i];
            MyPrettyMassive[0][3][newCounter0] = Z[i];
            outLess5 << X[i] << "\t" << Y[i] << "\t" << Z[i] << "\n";
            newCounter0++;
        }
        if (R < latticeParameter * parameter / 2)
        {
            MyPrettyMassive[1][0][newCounter1] = i;
            MyPrettyMassive[1][1][newCounter1] = X[i];
            MyPrettyMassive[1][2][newCounter1] = Y[i];
            MyPrettyMassive[1][3][newCounter1] = Z[i];
            outLess7 << X[i] << "\t" << Y[i] << "\t" << Z[i] << "\n";
            newCounter1++;
        }
        if (R > latticeParameter * ((parameter / 2 - 2) / 2 - 1) && R <= latticeParameter * ((parameter / 2 - 2) / 2 + 1))
        {
            MyPrettyMassive[2][0][newCounter2] = i;
            MyPrettyMassive[2][1][newCounter2] = X[i];
            MyPrettyMassive[2][2][newCounter2] = Y[i];
            MyPrettyMassive[2][3][newCounter2] = Z[i];
            outFrom2To3 << X[i] << "\t" << Y[i] << "\t" << Z[i] << "\n";

            BeforeRelax[0][newCounter2] = i;
            BeforeRelax[1][newCounter2] = X[i];
            BeforeRelax[2][newCounter2] = Y[i];
            BeforeRelax[3][newCounter2] = Z[i];
            newCounter2++;
        }
        if (R > latticeParameter * (parameter / 2 - 2) && R < latticeParameter * parameter / 2)
        {
            MyPrettyMassive[3][0][newCounter3] = i;
            MyPrettyMassive[3][1][newCounter3] = X[i];
            MyPrettyMassive[3][2][newCounter3] = Y[i];
            MyPrettyMassive[3][3][newCounter3] = Z[i];
            outFrom5To7 << X[i] << "\t" << Y[i] << "\t" << Z[i] << "\n";
            newCounter3++;
        }
    }
    outLess5.close(); // on this line the crash happends
    outLess7.close(); // sometimes on this
    outFrom2To3.close();
    outFrom5To7.close();

0 个答案:

没有答案