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

C++ 0x0F50DF58处未处理的异常:0xC0000005:访问冲突读取位置0x0047CA04,c++,exception,unhandled,C++,Exception,Unhandled,您好,我在调试后编写了以下代码,当我从(printdata())函数返回数据后尝试读取文件内部时,我目睹了以下错误,有人能帮我解决这个错误吗? 0x0F50DF58处的未处理异常:0xC0000005:访问冲突读取位置0x0047CA04 ////////////Library.h #include <string> using namespace std; class Library { private: string BookName; string Author; int Da

您好,我在调试后编写了以下代码,当我从(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;
}
///Library.h
#包括
使用名称空间std;
类库
{
私人:
字符串书名;
字符串作者;
int日、月、年;//日、月、年
浮动价格;
字符串主题;
双ISBN;
公众:
库(string=“”,string=“”,int=0,int=0,int=0,float=0.0,string=“”,double=0.0);
无效设定值(字符串);
void setAuthor(字符串);
作废setPurchaseDay(整数);
作废setPurchaseMonth(整数);
无效设置采购年度(整数);
无效设定价格(浮动);
void setSubject(字符串);
无效设置ISBN(双);
字符串getBookName();
字符串getAuthor();
int getPurchaseDay();
int getPurchaseMonth();
int getPurchaseYear();
浮动价格();
字符串getSubject();
双getISBN();
};
///////Linrary.cpp
#包括“stdafx.h”
#包括“Library.h”
#包括
#包括
使用名称空间std;
Library::Library(字符串书名、字符串作者、整数天、整数月、整数年、浮动价格、字符串主题、双isbn)
{   
书名;
作者(作者);
setPurchaseDay(天);
setPurchaseMonth(月);
设置采购年(年);
设定价格(价格);
设置主题(主题);
setISBN(isbn);
}
void库::setBookName(字符串bookname)
{
BookName=BookName;
}
void库::setAuthor(字符串作者)
{
作者=作者;
}
无效库::setPurchaseDay(整数天)
{
天=天;
}
无效库::setPurchaseMonth(整数月)
{
月=月;
}
无效库::setPurchaseYear(整数年)
{
年=年;
}
无效库::设置价格(浮动价格)
{
价格=价格;
}
void Library::setSubject(字符串主题)
{
主题=主题;
}
无效库::设置isbn(双isbn)
{
ISBN=ISBN;
}
///////////////////////////////////////////////
字符串库::getBookName()
{
返回书名;
}
字符串库::getAuthor()
{
返回作者;
}
int库::getPurchaseDay()
{
回归日;
}
int库::getPurchaseMonth()
{
返回月份;
}
int库::getPurchaseYear()
{
回归年;
}
浮动库::getPrice()
{
退货价格;
}
字符串库::getSubject()
{
返回主题;
}
双库::getISBN()
{
返回ISBN;
}
////////主要功能
#包括“stdafx.h”
#包括“Library.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int菜单();
void EnterData();
void PrintData();
int main(int argc,char*arg[])
{
对于(;;)
{
系统(“cls”);
int c=菜单();
开关(c)
{
案例1:
EnterData();
打破
案例2:
PrintData();
打破
案例8:
出口(0);
}//开关末端
}//结束
}
int菜单()
{
INTP;
做
{
cout免责声明我绝不是一个C程序员,在这里很可能是错的

您的问题可能来自这里:

fp.write((char*)(&data1), sizeof(Library));
您正在传递方法
data1
,但告诉它应该写入的文件大小是库的大小,其中应该是数据的大小


<代码>库< /C> >大于>代码> DATA1,将导致缓冲区溢出,在这里您将尝试读取应用程序/变量之外的内存地址。

我甚至更改为一个固定值,但错误继续。此外,在C++书中使用以下过程或方法。为什么要编写具有BO的库对象?带点“.”的okname?该逻辑使代码膨胀。请尝试删除该逻辑并调试代码。