如何修复.exe已停止工作

How do I fix .exe has stopped working?

本文关键字:停止工作 exe 何修复      更新时间:2023-10-16

在Visual Studios 2012中编写了这段C++代码,这只是任务的第一步。然而,当试图运行它时,它给了我.exe已经停止工作。我不确定为什么会出现这种情况,因为我以前使用过那个循环。知道为什么会发生这种事吗?

以下是正在读取的文件中的几行。

AA11 11AA Lee Caleb 1 1.01 2 2.01 3 5.01 01012000 1 01102000 p

ZZ33 33ZZ Wolfe Mitch 5 1.01 1 2.01 0 5.01 03051999 1 01112002 M

WW44 44WW Zeitouni Elie 10 1.01 5 2.01 10 5.01 05052012 0 05052013 M

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

struct record
{
    string custID, SPID, custLN, custFN;
    int Q1;
    double P1;
    int Q2;
    double P2;
    int Q3;
    double P3;
    string LOD;
    bool shipRec;
    string NCD, preMethod;
    double totalSales;
};

istream& operator >> (istream& in, record& r)
{
    in >> r.custID >> r.SPID >> r.custLN >> r.custFN >> r.Q1 >> r.P1 >>r.Q2 >> r.P2
       >> r.Q3 >> r.P3 >> r.LOD >> r.shipRec >> r.NCD >> r.preMethod;
    return in;
}
int main()
{
    ifstream inMaster;
    ifstream inTrans;
    inMaster.open("master.txt");
    inTrans.open("trans.txt");
    ofstream outNewM;
    ofstream outErrorL;
    outNewM.open("NewMaster.txt");
    outErrorL.open("errorLog.txt");
    record customer[100];
    int i=0;
    while (!inMaster.eof())
    {
        inMaster >> customer[i];
        customer[i].totalSales = customer[i].Q1 * customer[i].P1 + customer[i].Q2 * customer[i].P2 + customer[i].Q3 * customer[i].P3;
        i++;
    }
    inMaster.close();
    inTrans.close();
    outNewM.close();
    outErrorL.close();
    return 0;
}

问题是在某个时刻读取记录时出错。当这种情况发生时,流设置"故障位"以指示它处于错误状态,并且不会执行任何进一步的操作。eof测试仍然指示流不在末尾,但当您尝试读取时,由于流处于失败状态,因此不会发生任何事情。所以你保持循环,因为在那之后没有数据从流中读取。

执行输入后,添加以下内容:

if (inMaster.fail()) { 
    cerr << "Error reading line " << i+1 << endl; 
    return 1; 
}