Unhandled exception at 0x0F50DF58 : 0xC0000005: Access violation reading location 0x0047CA04

0

hi I write this following code after I debug when I try to read inside of my file after reading data when it returns from (printdata()) function i witness the following error can any one help me about this error? Unhandled exception at 0x0F50DF58 : 0xC0000005: Access violation reading location 0x0047CA04.

////////////Library.h
#include <string>
using namespace std;
class Library
{
private:
string BookName;
string Author;
int Day,Month,Year ;   //day  month year
float Price;
string Subject;
double ISBN;
public:
Library(string = "", string = "", int = 0, int = 0, int = 0, float = 0.0,     string = "", double = 0.0);   
void setBookName(string);
void setAuthor(string);
void setPurchaseDay(int );
void setPurchaseMonth(int);
void setPurchaseYear(int);
void setPrice(float);
void setSubject(string);
void setISBN(double);

string getBookName();
string getAuthor();
int getPurchaseDay();
int getPurchaseMonth();
int getPurchaseYear();
float getPrice();
string getSubject();
double getISBN();
};

///////Linrary.cpp
#include "stdafx.h"
#include "Library.h"
#include <iostream>
#include <string>
using namespace std;

Library::Library(string bookname, string author,int day,int month,int year, float price, string subject, double isbn)

{   
setBookName(bookname);
setAuthor(author);
setPurchaseDay(day);
setPurchaseMonth(month);
setPurchaseYear(year);
setPrice(price);
setSubject(subject);
setISBN(isbn);
}


void Library::setBookName(string bookname)
{
BookName = bookname;
}
void Library::setAuthor(string author)
{
Author = author;
}
void Library::setPurchaseDay( int day)
{
Day = day;
}
void Library::setPurchaseMonth(int month)
{
Month = month;
}
void Library::setPurchaseYear(int year)
{
Year = year;
}
void Library::setPrice(float price)
{
Price = price;
}
void Library::setSubject(string subject)
{
Subject = subject;
}
void Library::setISBN(double isbn)
{
ISBN = isbn;
}

///////////////////////////////////////////////
string Library::getBookName()
{
return BookName;
}
string Library::getAuthor()
{
return Author;
}
int Library::getPurchaseDay()
{

return Day;
}

int Library::getPurchaseMonth()
{
 return Month;
}
int Library::getPurchaseYear()
{
return Year;
}

float Library::getPrice()
{
return Price;
}
string Library::getSubject()
{
return Subject;
}
double Library::getISBN()
{
return ISBN;
}


////////main function
#include "stdafx.h"
#include "Library.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;

int menu();
void EnterData();
void PrintData();



int main(int argc, char *arg[])
{
for (;;)
{
    system("cls");
    int c = menu();
    switch (c)
    {
    case 1:
        EnterData();
        break;
    case 2:
        PrintData();
        break;
    case 8:
        exit(0);

    }//end of switch            
   }//end of for
 }

int menu()
{
int p;
do
{
    cout << "1. Enter Data of a Book." << endl;
    cout << "2. Print Data of all Books." << endl;
    cout << "3. Search a Book by Subject." << endl;
    cout << "4. Search a Book by Auther." << endl;
    cout << "5. Pirnt Books Purchased in a Period of Time." << endl;
    cout << "6. Search by ISBN." << endl;
    cout << "7. Price of Books Purchased in a Period of Time." << endl;
    cin >> p;       
} while (p < 0 || p>7);
cin.get();
return p;
}//end of menu() function


//***************Enter Data to File****************8
 void EnterData()
{
system("cls");
ofstream fp("LIB.txt", ios::out | ios::trunc);
if (!fp)
{
    cerr << "\nError in opening file to write in...";
    cin.get();
    exit(1);
}
Library data1;
cout << "\nEnter data of the book and write '.' instead of name to     finilize."<<endl<<endl;

string bookname, author, subject;
int day, month, year;
float price;
double isbn;



do{
    cout << "\nBook Name: ";
    getline(cin, bookname);
    data1.setBookName(bookname);

    if (bookname == ".")
    {
        author = ".";
        subject = ".";
        price = 0.0;
        isbn = 0;
        day = month = year = 0;
        data1.setAuthor(author);
        data1.setSubject(subject);
        data1.setPrice(price);
        data1.setISBN(isbn);
        data1.setPurchaseDay(day);
        data1.setPurchaseMonth(month);
        data1.setPurchaseYear(year);
        fp.write((char*)(&data1), sizeof(Library));
        return;
    }

    cout << "\nAuthor: ";
    getline(cin, author);
    data1.setAuthor(author);
    cout << "\nPurchase Date (day,month,year): ";
    cin >> day;
    cin >> month;
    cin >> year;
    cin.ignore();
    data1.setPurchaseDay(day);      
    data1.setPurchaseMonth(month);
    data1.setPurchaseYear(year);

    cout << "\nPrice: ";
    cin >> price;
    data1.setPrice(price);
    cin.ignore();
    cout << "\nSubject: ";
    getline(cin, subject);
    data1.setSubject(subject);

    cout << "\nISBN: ";
    cin >> isbn;
    data1.setISBN(isbn);
    cin.ignore();
    fp.write((char*)(&data1), sizeof(Library));
} while (1);        
fp.close();
cin.get();
 }//enter of enterdata() function

 //***************Print Data********************

 void PrintData()
{
system("cls");
ifstream fp("LIB.txt", ios::in);
if (!fp)
{
    cerr << "\nError in opening file to write in...";
    cin.get();
    exit(2);
}

Library data2;

string bookname, author, subject;
int day, month, year;   
float price;
double isbn;

cout << left << setw(12) << "BookName" << setw(12) << "Author" << setw(12) << "PurchaseDate"
<< right<<setw(7) << "Price" << setw(12) << "Subject" << setw(7) << "ISBN"<<endl;
cout << "____________________________________________________________________"<<endl;

fp.read((char *)(&data2), sizeof(Library));

while ( fp && !fp.eof())
{
    bookname=data2.getBookName();

    author=data2.getAuthor();
    day=data2.getPurchaseDay(); 
    month = data2.getPurchaseMonth();
    year = data2.getPurchaseYear();
    price = data2.getPrice();
    isbn = data2.getISBN();
    subject = data2.getSubject();
    //cout << left << setw(12) << bookname << setw(12) << author << setw(12) << purchasedate[0] << purchasedate[1] << purchasedate[2]
        //<< right << setw(7) << price << setw(12) << subject << setw(7) << isbn << endl;
    if (bookname.at(0) == '.')
    {
        fp.close();
        break;
    }

    fp.read((char *)(&data2), sizeof(Library));     
}

cin.get();
//fp.close();
//return;
}
c++
exception
unhandled
asked on Stack Overflow Jun 26, 2018 by R.A • edited Jul 6, 2020 by Martijn Pieters

1 Answer

0

disclaimer I am by no means a C programmer, and very well may be wrong here.

Your issues is probably coming from here:

fp.write((char*)(&data1), sizeof(Library));

You are passing the method data1, but telling it that the size of the file it should write is the size of Library, where it should be the size of data.

Library, being larger then data1, will cause a buffer overrun, where you will try reading a memory address outside of your application / variable.

answered on Stack Overflow Jun 26, 2018 by Matt Clark

User contributions licensed under CC BY-SA 3.0