我的代碼有問題,我不知道問題的確切原因。
Source.cpp
#include "class.h"
int main()
{
Book book1("At Mountain of Madness", "H.P Lovecraft", 1936);
book1.binaryFileWrite();
book1.binaryFileRead();
Book book2("Danwych's horror", "H.P Lovecraft", 1929);
book2.binaryFileWrite();
book2.binaryFileRead();
}
#ifndef BOOK_BINARY_LIBRARY
#define BOOK_BINARY_LIBRARY
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
class Book
{
public:
Book()
: _year(0)
{
_title = new char[10];
_author = new char[10];
strcpy(_title, "Undefined");
strcpy(_author, "Undefined");
}
Book(const char *title, const char *author, int year)
: _year(year)
{
_titleLen = strlen(title);
_authorLen = strlen(author);
_title = new char[_titleLen + 1];
_author = new char[_authorLen + 1];
strcpy(_title, title);
strcpy(_author, author);
}
Book(const Book& book)
{
_titleLen = strlen(book._title);
_authorLen = strlen(book._author);
_title = new char[_titleLen + 1];
_author = new char[_authorLen + 1];
strcpy(_title, book._title);
strcpy(_author, book._author);
}
~Book()
{
delete[] _title;
delete[] _author;
}
void display()
{
std::cout << _title << " " << _author << " " << _year << std::endl;
}
void binaryFileWrite(const std::string &fileName = "binaryLibrary.bin")
{
std::ofstream file(fileName, std::ios::binary);
if (!file.is_open()) {
std::cout << "Failed" << std::endl;
exit(1);
}
else {
/*file.write(_title, sizeof(_title));
file.write(_author, sizeof(_author));
file.write(reinterpret_cast<char*>(&_year), sizeof(int));*/
file.write(reinterpret_cast<char*>(this), sizeof(Book));
}
file.close();
}
void binaryFileRead(const std::string& fileName = "binaryLibrary.bin")
{
std::ifstream file(fileName, std::ios::binary);
if (!file.is_open()) {
std::cout << "Failed" << std::endl;
exit(2);
}
else {
Book read;
file.read(reinterpret_cast<char*>(&read), sizeof(Book));
std::cout << read._title << " " << read._author << " " << read._year << std::endl;
}
file.close();
}
private:
char* _title;
char* _author;
int _year{ 0 };
int _titleLen{ 0 };
int _authorLen{ 0 };
};
#endif
問題肯定出在代碼的這一部分:
void binaryFileRead(const std::string& fileName = "binaryLibrary.bin")
{
std::ifstream file(fileName, std::ios::binary);
if (!file.is_open()) {
std::cout << "Failed" << std::endl;
exit(2);
}
else {
Book read;
file.read(reinterpret_cast<char*>(&read), sizeof(Book));
std::cout << read._title << " " << read._author << " " << read._year << std::endl;
}
file.close();
}
它甚至打印出我想要的控制臺應(yīng)用程序,但仍然不返回0。
我不知道是什么問題,我嘗試了很多,但沒有任何幫助,我甚至認(rèn)為它不能從.bin
文件讀取到動態(tài)字符或其他東西。
順便說一句,我的第一個原型沒有使用動態(tài)數(shù)組,我使用了像_title[50]
和_author[50]
這樣大小不變的數(shù)組,效果很好。我想說完美,但當(dāng)我嘗試使用動態(tài)數(shù)組時,它失敗了。
主要問題是
Book
包含指向存儲在內(nèi)存其他位置的數(shù)據(jù)的指針。當(dāng)你讀寫一個Book
對象時,你讀寫的是那些指針as-is,而不是它們所指向的數(shù)據(jù)。因此,您需要分別處理char*
數(shù)據(jù)。而且,您的
binaryFileRead()
甚至沒有讀入調(diào)用它的Book
對象,而是讀入一個單獨的臨時Book
對象。請嘗試以下方法:
另一方面,您的代碼還有其他問題:
_titleLen
和_authorLen
字段。您確實應(yīng)該考慮使用std::string
來_title
和_author
字段,而不是使用char*
+int
。特別是因為您在代碼的其他地方使用了std::string
。_year
字段。Book
類不符合規(guī)則3,因為它缺少一個復(fù)制賦值運(yùn)算符。_title
和_author
字段切換為std::string
,則不需要實現(xiàn)復(fù)制構(gòu)造函數(shù)、復(fù)制賦值運(yùn)算符或析構(gòu)函數(shù),因為默認(rèn)的compiler-generated字段就足夠了。