C++ fstream 写入/读取对象
我有这些 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题是指针
如果你把它写出来并在程序的同一运行期间立即再次读入它,则指向的对象可能仍然存在。
如果您保存文件并有时稍后读取它,则
istruttore
对象很可能位于其他位置。Your problem is the pointer
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.好吧,只有当对象是 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 typecorso
?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)