1

I have made a class Flight with informations to be stored in a binary file called data.txt at another method.That saving of records was working fine, but now I'm having problems reading back the records I've saved. It is working to display all the records till the end of the file (eof). But when records are done displaying, there comes a pop up error saying that Program.exe has stopped working.

void Flight::ViewFlight(){
HANDLE hConsole;        //Console colors
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
fstream data;
Flight flight;
data.open("data.txt",ios::in | ios::binary);
if (data.fail())
{
    SetConsoleTextAttribute(hConsole, 12);
    cout<<"\n\nFlight data does not exist yet";
    cout<<"\n\nYou are being redirected to the Main Menu in 3 seconds\n\n";
    cout<<"3\n\n";
    Sleep(1000);
    cout<<"2\n\n";
    Sleep(1000);
    cout<<"1\n\n";
    Sleep(1000);
    cout<<"0\n\n";
    SetConsoleTextAttribute(hConsole, 15);
}
else{
    while(data.read((char*) &flight, sizeof(flight)))
    {   
        if(!data.eof())
        {
            SetConsoleTextAttribute(hConsole, 10);
            cout<<"\n\n----------- Record for "<<flight.flightid<<" -----------\n";
            SetConsoleTextAttribute(hConsole, 15);
            cout<<"\nFlight Number \t\t: "<<flight.flightnumber;
            cout<<"\nDeparture Airport\t: "<<flight.departAirport;
            cout<<"\nArrival Airport\t\t: "<<flight.arriveAirport;
            cout<<"\nDeparture Time\t\t: "<<flight.departTime.hour<<":"<<flight.departTime.minute;
            cout<<"\nDeparture Date\t\t: "<<flight.departDate.day<<"/"<<flight.departDate.month<<"/"<<flight.departDate.year;
            cout<<"\nPrice \t\t\t: RM "<<flight.price;
            cout<<"\nBusiness Class Seats\t: "<<flight.bseat;
            cout<<"\nFirst Class Seats\t: "<<flight.fseat;
            cout<<"\nEconomy Class Seats\t: "<<flight.totalseat;
            cout<<endl;
        }
    }
}
data.close();
}
James Leng
  • 19
  • 3

4 Answers4

4

Your Flight class contains std::string members. These are not plain old data types and typically hold pointers to dynamically allocated memory. You can't read and write your class as a unit and hope the std::string members and their contents will be properly constructed. The same may apply to the Time and Data members but you haven't shown how they're defined.

You need to look into proper serialization.

Several related questions:

Community
  • 1
  • 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • @JamesLeng: The bottom line is you can't do a binary file dump of a `std::string` object. It's a class that manages a resource (memory) and just writing the object itself doesn't include the allocated memory it points to. You'll need to read/write your class piece-by-piece. Added a few related SO questions to my answer. – Blastfurnace Sep 22 '13 at 19:28
0

The loop seems fine, your file might have corrupted data, probably a not terminated string, or may be there is/are some garbage characters at the end of the input file. to verify comment all the cout statements in the loop, and see if the program stops hanging. also data.eof() check is redundant, nevertheless it should not hang the program.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

just had a look at your flight class, you can not read directly into a class having other class objects. in your case, the string objects. you need to deserliaze the stream and initilize the variables yourself The problem is when the Flight is getting destructed it is destroying those string objects which are not correctly constructed string objects. Basically first separate your character strings from your buffer and assign those to your string variables one by one yourself.

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0

Flight structure consist of other classes that point to dynamically allocated memory (heap), for example string flightnumber; is STL string class, that has char* or wchar* inside. If you save Flight object as a binary buffer, it will save pointer only. While loading the object, you'll get memory address in the pointer, that is invalid. That's the reason you are getting access violation exception, that means you tried to access not allocated memory.

btw that's the best case you got 0xC0000005, in worst case you'd just access memory allocated for other objects, and got trash in your output.

You have to overload operator<< and operator>> for Flight class, and every class member which is not standard library. After you do it you'll just write:

fstream data;
Flight flight;
data >> flight;
Iuri Covalisin
  • 645
  • 4
  • 7