0

Image "EXCEPTION ERROR at the end of function" when I execute this function , everything goes fine,data is displayed but at the end,program stops saying like "YourFile.exe stopped working". and while debugging,enter image description here at the end of function,we get over to this exception type issue.

void readfn(const char* address, const int index) {
    system("cls"); char key;
    ifstream in; string line; int newindex = index; Book b2;
    in.open(address,ios::binary);
    if (in.is_open()) {
        in.seekg(0, ios::beg);
        cout << "\n tellg in readfn:" << in.tellg();
        in.read((char*)&b2, sizeof(Book));
        while (!in.eof()) {

            b2.display();
            in.read((char*)&b2, sizeof(Book));

        }
        in.close();
    }
    else {
        cout << "Error while opening File for Reading";
    }

    //cin.ignore();
    cout << "\n Press any key to return to Main Menu:";

    cin >> key;
    cin.ignore();
}

Image"DS ASSIGNMENT 1.exe stopped working"

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
user20913
  • 49
  • 9
  • Unrelated: `in.seekg(0, ios::beg);` should not be necessary. When you open the file it will point at the beginning of the file unless you explicitly instruct it not to. Congratulations on avoiding the bug that usually comes with `while (!in.eof())`. You can make this a bit easier with `while (in.read((char*)&b2, sizeof(Book)))`. – user4581301 Sep 24 '19 at 20:12
  • @user4581301 Thanks – user20913 Sep 24 '19 at 20:15
  • 1
    Not enough information to give you an answer, but here's a shot in the dark: Make sure `Book` only contains [POD](https://stackoverflow.com/questions/146452/what-are-pod-types-in-c) members. Anything more complicated will fail horribly if read from a file as a binary object. – user4581301 Sep 24 '19 at 20:17
  • @user4581301 Making Book contain only POD members doesn't work – user20913 Sep 24 '19 at 20:25
  • Nothing in the code jumps out at me as a good place to crash. I recommend backing up your code and hacking out a [mcve]. If making the [mcve] doesn't result in a face-palm and you fixing the bug on your own, add the [mcve] to the question. – user4581301 Sep 24 '19 at 20:31

0 Answers0