C++對動態(tài)字符的二進(jìn)制讀取

我的代碼有問題,我不知道問題的確切原因。

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對象。

請嘗試以下方法:

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); // TODO: throw an exception instead...
    }
    binaryWrite(file);
}

void binaryWrite(std::ostream &out)
{
    out.write(reinterpret_cast<char*>(&_titleLen), sizeof(_titleLen));
    out.write(_title, _titleLen);

    out.write(reinterpret_cast<char*>(&_authorLen), sizeof(_authorLen));
    out.write(_author, _authorLen);

    out.write(reinterpret_cast<char*>(&_year), sizeof(_year));
}

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); // TODO: throw an exception instead...
    }
    binaryRead(file);
}

void binaryRead(std::istream &in)
{
    int len;

    in.read(reinterpret_cast<char*>(&len), sizeof(_len));
    char *s = new char[len + 1];
    in.read(s, len);
    delete[] _title; _title = s;
   _titleLen = len;

    in.read(reinterpret_cast<char*>(&len), sizeof(_len));
    s = new char[len + 1];
    in.read(s, len);
    delete[] _author; _author = s;
   _authorLen = len;

    in.read(reinterpret_cast<char*>(&_year), sizeof(_year));
}

另一方面,您的代碼還有其他問題:

  • 默認(rèn)構(gòu)造函數(shù)沒有初始化_titleLen_authorLen字段。您確實應(yīng)該考慮使用std::string_title_author字段,而不是使用char*+int。特別是因為您在代碼的其他地方使用了std::string。
  • 您的復(fù)制構(gòu)造函數(shù)沒有復(fù)制_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字段就足夠了。
主站蜘蛛池模板: 日韩成人无码一区二区三区 | 国产日产久久高清欧美一区| 一区二区三区在线| 免费国产在线精品一区| av在线亚洲欧洲日产一区二区| 狠狠综合久久av一区二区| 亚洲精品色播一区二区| 狠狠爱无码一区二区三区| 久久AAAA片一区二区| 农村乱人伦一区二区| 人妻精品无码一区二区三区| 国产大秀视频一区二区三区| 久久精品国产一区二区三区不卡| 无码国产精品一区二区免费式芒果 | 亚洲综合色自拍一区| 亚洲国产成人久久综合一区| 日韩在线观看一区二区三区| 国产乱码伦精品一区二区三区麻豆 | 成人无码精品一区二区三区| 一区二区三区四区精品| 3d动漫精品一区视频在线观看| 日本精品啪啪一区二区三区| 嫩B人妻精品一区二区三区| 国产精品分类视频分类一区| 国产福利电影一区二区三区久久久久成人精品综合 | 无码人妻一区二区三区在线视频| 中文字幕av人妻少妇一区二区| 影院无码人妻精品一区二区| 中文字幕亚洲综合精品一区| 精品一区中文字幕| 久久精品国产第一区二区三区| 国产一区精品视频| 中文字幕一区二区精品区| 亚洲Aⅴ无码一区二区二三区软件| 老熟妇仑乱一区二区视頻| 亚洲一区二区高清| 五十路熟女人妻一区二区| 国产成人无码AV一区二区在线观看 | 亚洲一区二区在线免费观看| 美女免费视频一区二区| 亚洲福利电影一区二区?|