1

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?

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
Claudio Pomo
  • 2,392
  • 7
  • 42
  • 71
  • Can you show how you open the files? – Some programmer dude Jan 24 '12 at 12:53
  • fstream fcorso("corso.dat",ios::in|ios::binary); – Claudio Pomo Jan 24 '12 at 12:59
  • It seem you write the bytes of a structure containing a pointer to the data you actually want. That is, you probably want to write the data pointed to by this pointer. That said, please note that writing a bunch of bytes isn't a binary format. It is a collection of unstructured byres bound to give you problems rather sooner than later although it may work short term any change to the system, the compiler, or the platform can render the data useless. You should use formated reads and writes, if necessary using a binary format. – Dietmar Kühl Jan 24 '12 at 13:09

2 Answers2

1

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

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)

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434