C++ fstream 写入/读取对象

发布于 2024-12-28 21:55:34 字数 2133 浏览 2 评论 0原文

我有这些 C++ 标头,

#include <iostream>
#include "istruttore.h"
#define max 30

using namespace std;
//==============================================================================
class corso
{
  friend ostream& operator << (ostream& out, const corso& corsoapp);
  friend istream& operator >> (istream& in, corso& corso);

  public:
       int getid_corso();
       char* getnomecorso();
       double getcosto();
       char* getdurata(); 
       int getistruttore_id();
       char* getistruttore_name();
       char* getistruttore_surname();
       void setid_corso(int course);
       void setnomecorso(char namecourse[]);
       void setcosto (double pay);
       void setdurata(char duration[]); 
       void set_istruttore(char name[], char surname[], int id, int id_corso);
       corso();
       ~corso();
       istruttore* ist;
   private:
       int id_corso;
       char nomecorso[max];
       double costo;
       char durata[max];
    };



#include <iostream>

#define max 30

using namespace std;
//==============================================================================
class istruttore {
friend ostream& operator <<(ostream& out, const istruttore& istruttoreapp);
friend istream& operator >>(istream& in, istruttore& istruttore);

public:
 int getid_istruttore();
 char* getnome();
 char* getcognome();
 int getid_corso();
 void setid_istruttore(int idistruttore);
 void setnome(char name[]);
 void setcognome(char surname[]);
 void setid_corso(int idcorso);
 istruttore();
 ~istruttore();
protected:
 int id_istruttore;
 char nome[max];
 char cognome[max];
 int id_corso;
  };

我已经实现了这两个标头的所有方法。我想用这种方式编写一个二进制文件:

fcliente.write(reinterpret_cast<const char*>(&tmpcorso),sizeof(tmpcorso));

其中 tempcorso 是 corso 类型的对象。写作可以,但阅读不行。 我尝试使用此代码读取相同的二进制文件

fcorso.read(reinterpret_cast<char*>(&tmpcorso),sizeof(tmpcorso))

,但是当我查看 tmpcorso 中的 istruttore 的值时,该值不正常。我该如何修复它?

I have these C++ headers

#include <iostream>
#include "istruttore.h"
#define max 30

using namespace std;
//==============================================================================
class corso
{
  friend ostream& operator << (ostream& out, const corso& corsoapp);
  friend istream& operator >> (istream& in, corso& corso);

  public:
       int getid_corso();
       char* getnomecorso();
       double getcosto();
       char* getdurata(); 
       int getistruttore_id();
       char* getistruttore_name();
       char* getistruttore_surname();
       void setid_corso(int course);
       void setnomecorso(char namecourse[]);
       void setcosto (double pay);
       void setdurata(char duration[]); 
       void set_istruttore(char name[], char surname[], int id, int id_corso);
       corso();
       ~corso();
       istruttore* ist;
   private:
       int id_corso;
       char nomecorso[max];
       double costo;
       char durata[max];
    };



#include <iostream>

#define max 30

using namespace std;
//==============================================================================
class istruttore {
friend ostream& operator <<(ostream& out, const istruttore& istruttoreapp);
friend istream& operator >>(istream& in, istruttore& istruttore);

public:
 int getid_istruttore();
 char* getnome();
 char* getcognome();
 int getid_corso();
 void setid_istruttore(int idistruttore);
 void setnome(char name[]);
 void setcognome(char surname[]);
 void setid_corso(int idcorso);
 istruttore();
 ~istruttore();
protected:
 int id_istruttore;
 char nome[max];
 char cognome[max];
 int id_corso;
  };

I have implemented all methods for both headers. I want to write a binary file in this way:

fcliente.write(reinterpret_cast<const char*>(&tmpcorso),sizeof(tmpcorso));

where tempcorso is an object of type corso. Writing is ok, but reading isn't ok.
I try to read the same binary file with this code

fcorso.read(reinterpret_cast<char*>(&tmpcorso),sizeof(tmpcorso))

but when I look at the value of istruttore in tmpcorso the value isn't ok. How do I fix it?

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

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

发布评论

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

评论(2

那片花海 2025-01-04 21:55:34

你的问题是指针

   istruttore* ist;

如果你把它写出来并在程序的同一运行期间立即再次读入它,则指向的对象可能仍然存在。

如果您保存文件并有时稍后读取它,则 istruttore 对象很可能位于其他位置。

Your problem is the pointer

   istruttore* ist;

If you write it out and immediately read it in again during the same run of the program, the pointed-to object might still be there.

If you save the file and read it in sometimes later, it is highly likely that the istruttore object is somewhere else.

萌化 2025-01-04 21:55:34

好吧,只有当对象是 POD。如果删除用户定义的构造函数和析构函数,则可以使您的类成为 POD。当然,只有当它们实际上没有做任何有用的事情时,您才应该这样做。如果是这样,那么为什么不使用运算符 >><< 来读取和写入 corso

请注意,如果您遵循 Claudio Pomo 的评论,并且您的代码似乎可以正常工作,它仍然会导致未定义的行为(请阅读详细信息< a href="https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special">在我已经提供的链接中)

Well, the trick with writing an object as binary data and then reading it back can be done only when the object is a POD. You can make your class a POD if you remove your user-defined constructor and destructor. Of course, you should do that only if they don't actually do anything useful. If they do, then why don't you use your operators >> and << to read and write objects of type corso?

Please note that if you follow Claudio Pomo's comment and your code appears to work, it still results in Undefined behavior (read the details in the link I already provided)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文