write an object of user defined class into file.
-
How to write and read an object of user defined class say ABC into file.
-
How to write and read an object of user defined class say ABC into file.
Depends what you are using. Are you using the MFC's ? If yes you can look at serialization. And please, be more specific when you ask a question.
-
How to write and read an object of user defined class say ABC into file.
Hi anilksingh, I used to write objects of my class, in the C way, using "fwrite". Here is the code snippet: Regards, Sarvan AL
class Student { int regno; char name[30] ; public: Student() { regno = 0; strcpy(name, "NULL") ; } Student(int rno, char *tname) { regno = rno ; strcpy(name, tname) ; } void Display() { cout<<"Regno: " << rno << endl ; cout<<"Name: " << name << endl ; } } ; main() { FILE *fp = fopen("Test.dat", "wb") ; Student s1(1, "Robert"), s2(2, "Smith") ; fwrite(&s1, sizeof(Student), 1, fp) ; fwrite(&s2, sizeof(Student), 1, fp) ; fclose(fp) ; Student t ; fp = fopen("Test.dat", "rb") ; fread(&t, sizeof(Student), 1, fp) ; t.Dispaly() ; fread(&t, sizeof(Student), 1, fp) ; t.Dispaly() ; fclose(fp) ; }