Reputation: 1232
I am writing a c++ program to write data into a file in binary mode and read it from the file. I am writing an object and reading to an object. The problem I am facing is, when I write into the file and read it in that instance without closing the program, it works file. But after the program terminates execution, and comment out the writing code block and try to read the already written file, I get crappy output.
I am not able to understand what is going wrong.
Here is the code:
#include <fstream.h>
#include <string.h>
class Student{
protected:
char *Name, *Sub_code;
int Roll;
public:
Student(){}
};
class Details:private Student{
private:
char *Sub_Name;
int internal_marks, external_marks;
/*Methods*/
void setName();
void setRoll();
void setSubCode();
void setSubName();
void setInternalMarks();
void setExternalMarks();
public:
Details(){
Name = new char[1];
Sub_code = new char[1];
Sub_Name = new char[1];
Name[0] = '\0';
Sub_code[0] = '\0';
Sub_Name[0] = '\0';
internal_marks = 0;
external_marks = 0;
Roll = 0;
}
void setDetails();
void getDetails();
static void writeDetails(Details detail);
static void readDetails();
};
void Details::setName(){
cout<<"Enter Student Name : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Name = new char[len];
strcpy(Name,tmp);
}
void Details::setRoll(){
cout<<"Enter Roll Number : ";
cin>>Roll;
}
void Details::setSubCode(){
cout<<"Enter Subject Code : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Sub_code = new char[len];
strcpy(Sub_code,tmp);
}
void Details::setSubName(){
cout<<"Enter Subject Name : ";
char tmp[100];
tmp[0] = '\0';
cin>>tmp;
int len = strlen(tmp);
Sub_Name = new char[len];
strcpy(Sub_Name,tmp);
}
void Details::setInternalMarks(){
cout<<"Enter internal marks : ";
cin>>internal_marks;
}
void Details::setExternalMarks(){
cout<<"Enter external marks : ";
cin>>external_marks;
}
void Details::setDetails(){
setName();
setRoll();
setSubCode();
setSubName();
setInternalMarks();
setExternalMarks();
}
void Details::getDetails(){
cout<<Name<<"\t\t";
cout<<Roll<<"\t\t";
cout<<Sub_code<<"\t\t";
cout<<Sub_Name<<"\t";
cout<<internal_marks<<"\t";
cout<<external_marks<<"\t\n";
}
void Details::writeDetails(Details detail){
ofstream os("StudentsRecord.dat", ios::binary|ios::ate);
os.write(reinterpret_cast <char *>(&detail),sizeof(detail));
os.close();
}
void Details::readDetails(){
Details detail;
ifstream is("StudentsRecord.dat", ios::binary|ios::in|ios::beg);
cout<<"Name\tRoll\tSubject Code\tSubject Name\tInternal marks\tExternal Marks\n";
while (is.read(reinterpret_cast<char *>(&detail), sizeof(detail))){
detail.getDetails();
}
is.close();
}
int main(){
Details y,x;
/*for(int i = 0; i < 2; i++){
x.setDetails();
Details::writeDetails(x);
}*/
Details::readDetails();
return 0;
}
The commented code in the main() is the block that is used for writing the data in the file. Here is the sample screen shot of the output I am getting.
Regards Priyabrata
Upvotes: 0
Views: 515
Reputation: 11968
The Detail
class has char*
inside. When you write it to the file you write the actual pointer address of the string, not the actual string. When you read it back in the same program it works because the data is still there.
When you rerun your program you get garbage because that pointer is not at some random piece of memory.
You should use a library that does serialization for you. It's kinda hard to get it right. Take a look at https://code.google.com/p/protobuf/ , but I'm sure there are other ones as well.
Upvotes: 2