c++什么 fstream write 函数写入文件(post 中的 coden)
我不明白文件 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据推测,您引用的函数是
类 BOOK
的成员函数,并且write
调用将简单地转储当前BOOK
的整个二进制表示形式> 实例到文件中。 (this
的类型为BOOK*
。)这通常不是一件非常可移植或明智的事情,因为序列化数据的未来消费者无法知道实际的序列化数据。序列化格式。 (未来的消费者可能是你自己在不同的机器或不同的编译器上。)如果你想认真对待这个问题,请查找正确的序列化策略。
Presumably the function you have quoted is a member function of a
class BOOK
, and thewrite
call will simply dump the entire binary representation of the currentBOOK
instance into the file. (this
is of typeBOOK*
.)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.