(c++)在title.exe[6284]中出现fstream win32异常

(C++) fstream win32 exception occured in title.exe [6284]

本文关键字:fstream 异常 win32 6284 c++ title exe      更新时间:2023-10-16

所以我做了以下算法来解决一个问题,但我不明白为什么当我使用fstream而不是iostream时它会崩溃。当我使用较低的数字时,例如(输入例如:1 4 5),它与fstream完美地工作,但如果我使用一些较大的输入数字(例如:3 987654300 210),它会说"进程返回-1073741819 (0xC0000005)"。如果我使用iostream,即使是9位输入,它也能很好地工作。

是的,我有parola。In和parola。Out与.cpp/.exe在同一个文件夹中,in包含输入数字和所有内容。

忽略算法和注释,只需注意"fout"answers"fin"的使用。

#include <fstream>
#include <iostream>
using namespace std;
int main()
{
    ifstream fin("parola.in");
    ofstream fout("parola.out");
    int a,b,k; //VARIABILE ORIGINALE
    int ord=1; //NUMARUL DE ORDINE AL VECTORILOR
    int cifa=0,cifb=0; //NUMARUL DE CIFRE AL NUMERELOR A SI B
    int nrA[18],nrB[10]; //VECTORII CE CONTIN CIFRELE NUMERELOR A SI B
    int temp[9]; //VECTORUL CE CONTINE CIFRELE NR. A IN ORDINE INVERSA
    fin>>k>>a>>b;
    while (a>0)
    {
        temp[ord]=a%10;
        a/=10;
        ord++;
        cifa++;
    }
    ord=1; //RESET NR ORDINE
    while (b>0)
    {
        nrB[ord]=b%10;
        b/=10;
        ord++;
        cifb++;
    }
    for (int topkek=1; topkek<=cifa; topkek++)
    {
        nrA[topkek]=temp[cifa+1-topkek]; //ORDONAREA CIFRELOR LUI A CRESCATOR, IN VECTORUL nrA
    }
    for (int zomtan=1; zomtan<=k; zomtan++)
    {
        fout<<nrA[zomtan];
        nrA[zomtan]=-1;
    }
    fout<<nrB[1];
    nrB[1]=-1;
    for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
    {
        nrA[zomtan2]=-1;
    }
    for (int zomtan3=cifb+1; zomtan3<=10; zomtan3++)
    {
        nrB[zomtan3]=-1;
    }
    for (ord=1; ord<=9; ord++)
    {
        if (nrA[ord+k]!=-1)
        {
            fout<<nrA[ord+k];
        }
        if (nrB[ord+1]!=-1)
        {
        fout<<nrB[ord+1];
        }
    }
    return 0;
}

可能会出现问题,因为我认为,您使用基于1的数组而不是基于属性0的

for (int zomtan2=cifa+1; zomtan2<=18; zomtan2++)
{
    nrA[zomtan2]=-1;
}

问题总是会发生,因为你写了zomtan2<=18

和你的数组声明是int nrA[18],我之前说过总是会导致问题。

这些错误在你的代码中无处不在,所以我建议你重写程序