0x0F50DF58处未处理的异常:0xC0000005:访问冲突读取位置0x0047CA04

时间:2018-06-26 22:51:38

标签: c++ exception access unhandled

嗨,我在调试后写了以下代码,当我尝试从(printdata())函数返回的数据读取数据后读取文件内部时,我看到以下错误可以帮助我解决此错误吗? 0x0F50DF58的未处理异常:0xC0000005:访问冲突读取位置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;
}

1 个答案:

答案 0 :(得分:0)

免责声明我绝不是C程序员,这很可能是错误的。

您的问题可能来自这里:

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

您正在传递方法data1,但告诉它应该写入的文件的大小是Library的大小,而应该是数据的大小。

Library大于data1,将导致缓冲区溢出,您将尝试在其中读取应用程序/变量之外的内存地址。