I am a complete beginner in C++, and I am asked things that are way more difficult than what I have learned.
I am trying to read information from a binary file, (InfoFile.dat) and display it on the console.
The program is reading the info and displaying it on the console with no problem, and I can even continue using the program normally.
However, after I exit the program using: return EXIT_SUCCESS
a window appears and this error shows:
Unhandled exception at 0x542FCCC8 (msvcp110d.dll) in pcp.exe:
0xC0000005: Access violation reading location 0x00474EB4.
The error only shows when I try reading from the file, not writing.
And only when I exit the program using return EXIT_SUCCESS
or exit(1)
(not when I close the console window from the x button)
Please know that I have googled, looked at many question in stackoverflow with the same error, but none of them helped
Code:
Display function:
driverObj
is an object of the class driver
void Staff::displayDriver(){
int i=1;
ifstream fp1;
fp1.open("InfoFile.dat",ios::binary);
if(fp1.is_open()){
while(!fp1.eof())
{
fp1.read((char*)&driverObj,(sizeof(driverObj)));
cout << i << ". ";
driverObj.showDriver();
i++;
}
}
else{
cout << "File was not found!" << endl;
}
fp1.close();
}
Show function
void Driver::showDriver(){
cout << "ID: " << id << " - Name: " << name << " - Zipcode: " << zipcode << " - Phone: " << phone << endl;
}
Driver class
class Driver : public User {
protected:
string name;
long phone;
int id, zipcode;
Vehicle vehicle;
public:
Driver();
Driver(int,string,int ,long , Vehicle);
void showDriver();
int getID();
};
Also, visual studio shows me this in "xutility" after it breaks:
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
(*_Pnext)->_Myproxy = 0;