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();
}
4
  • 1
    The definition of Flight would be relevant. Commented Sep 22, 2013 at 15:53
  • codepad.org/LTSr5bCB Commented Sep 22, 2013 at 17:46
  • Run your program in a debugger and see where it fails. Commented Sep 22, 2013 at 17:47
  • Unhandled exception at 0x0FABCCC8 (msvcp110d.dll) in System.exe: 0xC0000005: Access violation reading location 0x004C0754. This popped up when I tried debugging it, it was after reading the last record. Commented Sep 22, 2013 at 17:57

4 Answers 4

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:

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
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.

1 Comment

tried commenting on all the cout statements, still has the same error.
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.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.