c++什么 fstream write 函数写入文件(post 中的 coden)

发布于 2024-12-18 06:39:59 字数 460 浏览 2 评论 0原文

我不明白文件 file.write((char *) this, sizeof(BOOK)) ; 写入了什么内容。请解释一下:)

void add_new_book(int tcode,char tname[33], char tauthor[26], float tprice, int tcopies, int tavail)
{
    fstream file ;
    file.open("BOOK.DAT", ios::app) ;
    bookcode = tcode ;
    strcpy(name,tname) ;
    strcpy(author,tauthor) ;
    price = tprice ;
    copies = tcopies ;
    avail = tavail ;
    file.write((char *) this, sizeof(BOOK)) ; }

I'm don't understand what is written to a file file.write((char *) this, sizeof(BOOK)) ;. Please explain :)

void add_new_book(int tcode,char tname[33], char tauthor[26], float tprice, int tcopies, int tavail)
{
    fstream file ;
    file.open("BOOK.DAT", ios::app) ;
    bookcode = tcode ;
    strcpy(name,tname) ;
    strcpy(author,tauthor) ;
    price = tprice ;
    copies = tcopies ;
    avail = tavail ;
    file.write((char *) this, sizeof(BOOK)) ; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生死何惧 2024-12-25 06:39:59

据推测,您引用的函数是类 BOOK 的成员函数,并且 write 调用将简单地转储当前 BOOK 的整个二进制表示形式> 实例到文件中。 (this 的类型为 BOOK*。)

这通常不是一件非常可移植或明智的事情,因为序列化数据的未来消费者无法知道实际的序列化数据。序列化格式。 (未来的消费者可能是你自己在不同的机器或不同的编译器上。)如果你想认真对待这个问题,请查找正确的序列化策略。

Presumably the function you have quoted is a member function of a class BOOK, and the write call will simply dump the entire binary representation of the current BOOK instance into the file. (this is of type BOOK*.)

This is usually not a very portable or sensible thing to do, since future consumers of the serialized data have no way of knowing the actual serialization format. (The future consumer may be yourself on a different machine or a different compiler.) Look up proper serialization strategies if you want to take this seriously.

怎樣才叫好 2024-12-25 06:39:59
    void add_new_book (BOOK &book){    // call the function as: add_new_book(book1);
        fstream file ;                 // where book1 is an object of class BOOK
        file.open("BOOK.DAT", ios::app) ;
        bookcode = book.bookcode ;
        strcpy(name,book.name) ;
        strcpy(author,book.author) ;
        price = book.price ;
        copies = book.copies ;
        avail = book.avail ;
        file.write((char *)this, sizeof(BOOK)) ;
        file.close() ;    //don't forget to close the file 
    }
    void add_new_book (BOOK &book){    // call the function as: add_new_book(book1);
        fstream file ;                 // where book1 is an object of class BOOK
        file.open("BOOK.DAT", ios::app) ;
        bookcode = book.bookcode ;
        strcpy(name,book.name) ;
        strcpy(author,book.author) ;
        price = book.price ;
        copies = book.copies ;
        avail = book.avail ;
        file.write((char *)this, sizeof(BOOK)) ;
        file.close() ;    //don't forget to close the file 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文