n Make sure that it does not exceed the buffer.
In save() :
int n=strlen(professeur);
n should be no more than 59 here - you need to check.
In load() :
i.read((char *)&n, sizeof(int));
It is better to check n (max. 59).
also:
int n=strlen(professeur); char buff[60], *buff2; o.write((char *)&n, sizeof(int)); strcpy(buff, getProfesseur()); o.write(buff, n+1);
Two different values ββare used to write data: strlen(professeur) , and then getProfesseur() .
You also do not allocate memory for professeur (at least not in the code shown). Thus strcpy(professeur, buff); in load() doesn't work either.
You can also change:
private: char* professeur;
to
private: char professeur[60];
Thus, you should not have allocate and deallocate memory yourself.
And make sure all lines have zero termination.
Of course, if the exercise allows this, you can use string ( string professeur; ) instead, and the data stream in and using << and >> .
source share