exception:加载文件中保存的对象时无效的nulll指针

时间:2018-12-29 17:23:31

标签: c++ exception null

我一直试图将整个结构数据保存到文件中,然后从文件中读取,您可以在我的代码中看到我正在将数据写入struct(由用户输入),然后将其保存到使用myFile.write函数保存对象的文件

如下面的代码所示,当程序第一次运行时,我再次输入菜单后又输入数据,然后按2加载数据,它可以正常工作并成功加载数据(我认为)。返回菜单,然后按3并显示我创建的所有航班(显然是因为数据当前位于主内存中,并且尚未由文件准备好)

    //Global Variables
    string savedFlight = "airportsDB\\SavedFlights.txt"; //path
    const unsigned short NUMBER_OF_AIRPORTS = 8;
    int gFlightSize=0;
    ifstream rFlightData;
    ofstream wFlightData;
    //SKipped some part
    struct flight {
    airport depature;
    airport arrival;
    double crsAlt;
    double distance;
    double fuel;
    void flight::createFlight();
    void flight::showFlight();
};

    void flight::createFlight() {
            cout << "Please insert depature airport (ICAO) : ";
            cin>>depature.icao;

        if(searchapt(depature.icao)){
            cout << "Please insert arrival airport (ICAO) : ";
            cin>>arrival.icao;

                if(searchapt(arrival.icao)){
                    cout<<"Enter Cruise Altitude [3000<Altitude<50000]  : ";
                    cin>>crsAlt;

                    if(crsAlt>50000 || crsAlt<3000){
                    cout<<"Unable CruiseAltitude Please put DIGITS* between 50,000ft and 3,000ft ";
                    }
                    else{
                        //CNTINUE HERE
                    }
            }

            else{
            cout<<"Arrival Airport Does Not Exist In DataBase";
            }
        }
        else{
            cout<<"Departure Airport Does Not Exist In DataBase";
        }
}

    void flight::showFlight(){
                        cout<<"\n Departure Airport : "<<depature.icao
                        <<"\n Arrival   Airport : "<<arrival.icao
                        <<"\n Cruise Altitude   : "<<crsAlt
                        <<"\n\n__________________________________________________\n\n";
    }

    //(Skipped some part)
        void writeFlightData(){
        int i=gFlightSize;
        cout<<"How many flights do you want to add : ";
        cin>>gFlightSize;
        saveCounter(gFlightSize);

    flights=new flight[gFlightSize];

    wFlightData.open(savedFlight,ios::app);

    textColor(12);
    cout<<"USE UPPERCASE LETTER !\n";
    textColor(15);

    while(i<gFlightSize){
            flights[i].createFlight();
            wFlightData.write((char *)&flights[i], sizeof(flights[i]));
            i++;
    }
    wFlightData.close();
}

void loadFlightData(){

    rFlightData.open(savedFlight);// , ios::in); // open file for reading

    cout << "  Loading Saved flights from disk";
    for(int i=3;i>0;i--){ //REVERSE LOOP FOR A TWIST
                cout<<" . ";
                Sleep(800);
            }
    cls 

    if(rFlightData.fail()){
        cout << "\nAn error occured while loading Saved Flights from disk .The file is corrupt or does not exist on the specified path\n\a"<<"\n Press any key to go back to the menu";
        saveCounter(0);
        _getch();
        cls
    }
    else{   

        Sleep(1000);
        for (int i = 0; i < gFlightSize; i++) {
            rFlightData.read((char *)&flights[i], sizeof(flights[i])); // read an object from a file
        }
        cout<<"  Data Load Successfull!  ";
        rFlightData.close(); // close the file
    }
}

我正在保存一个加法计数器,该计数器在程序再次启动时会读取,并且知道我添加的航班数量

现在的问题是,当我关闭该程序并再次运行并按2加载数据时,它不会加载数据,但会崩溃并给出异常错误“无效的空指针”

*  调试断言失败! 程式C:\ Windows \ SYSTEM32 \ MSVCP110D.dll 文件:f \ dd \ vctools \ crt_bld \ self_x86_ \ crt \ src \ istream 线:734

表达式:无效的空指针

有关如何...的信息 (按“重试”以调试应用程序)

*

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,对不起,这个问题还不清楚。 这是一个逻辑错误,因为我使用的是动态结构数组 在尝试加载数据时类似地从用户那里获取数据时,我没有为指针分配新的动态位置,可以在该位置创建结构数组,然后将数据加载到程序中的结构中,所以我要做的就是写
flights=new flight[gFlightSize];

在这样的loaddata函数中,因此在加载动态位置之前,可以在主内存中将动态位置分配给可以将数据加载到

的结构
void loadFlightData(){
flights=new flight[gFlightSize];
rFlightData.open(savedFlight);// , ios::in); // open file for reading

cout << "  Loading Saved flights from disk";
for(int i=3;i>0;i--){ //REVERSE LOOP FOR A TWIST
            cout<<" . ";
            Sleep(800);
        }
cls 

if(rFlightData.fail()){
    cout << "\nAn error occured while loading Saved Flights from disk .The file is corrupt or does not exist on the specified path\n\a"<<"\n Press any key to go back to the menu";
    saveCounter(0);
    _getch();
    cls
}
else{   

    Sleep(1000);
    for (int i = 0; i < gFlightSize; i++) {
        rFlightData.read((char *)&flights[i], sizeof(flights[i])); // read an object from a file
    }
    cout<<"  Data Load Successfull!  ";
    rFlightData.close(); // close the file
}
}