1

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;
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
PianomaR
  • 43
  • 1
  • 10
  • 2
    `while(!fp1.eof())` Who taught you to do this? – Lightness Races in Orbit May 30 '15 at 18:51
  • 2
    _Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve)._ – πάντα ῥεῖ May 30 '15 at 18:52
  • @LightnessRacesinOrbit I have tried removing it and putting it as `while(fp1.read(char*)&driverObj,sizeof(driverObj));` and didn't solve the issue – PianomaR May 30 '15 at 18:54
  • @PianomaR: It solved _one_ issue, even if you didn't notice. Consider that you may have multiple problems with your code, and your `while` loop as it is in the question is just plain wrong! I agree it's unlikely to be the causing the crash, but it'll cause (at best) extra output. As for the crash itself, we do not have enough information. Time to present the _testcase_ that you were taught about when you signed up to the site. – Lightness Races in Orbit May 30 '15 at 18:55
  • @LightnessRacesinOrbit thank you, it has improved the output, as it was repeating one of the lines. Could you please guide me to where I can read about what I am supposed to provide so you can have the complete information? Thank you very much – PianomaR May 30 '15 at 19:04
  • http://stackoverflow.com/q/5605125/560648 – Lightness Races in Orbit May 30 '15 at 19:04
  • @PianomaR Read [this](http://www.sscce.org). – Baum mit Augen May 30 '15 at 19:06
  • An instance of `Driver` contains a `std::string`. A `std::string` typically contains a pointer to dynamically allocated memory. Reading that as binary data from a file, you get an arbitrary pointer value that usually doesn't make sense in this process. – Cheers and hth. - Alf May 30 '15 at 19:32
  • Beginners should probably not use `eof`, casts or pointers. – Kerrek SB May 30 '15 at 20:20
  • @Cheersandhth.-Alf I have tried changing it to `char[20]` and the exception is still there. I have removed the whole string variable (as a test) but the exception persisted. – PianomaR May 30 '15 at 20:38
  • With a changed layout you need to generate a new file. Also, there's a `Vehicle` (which could be problematic) as well as a `User` base class sub-object (ditto). But generally, just don't do binary i/o. It can be more efficient yes. But as you've discovered, it's also fraught with danger. – Cheers and hth. - Alf May 30 '15 at 21:06

0 Answers0