I am having trouble writing and reading array of objects.
this program is representing the trouble that I am having in real one.
I want to write class objects in a file "Stu.txt" and then read it so that i can display its contents the program is giving garbage value after 3 objects.
i don't know why? I have search through net but can't find any solution.
I don't have any idea about the problem.
I am using borland 5.0.2 in windows xp sp2
And one more thing why the contents of Stu.txt are displayed in form of symbols
that too only values of those variables which are not char?
I have provided the output just in case.
Please help me...
Thanks in advance.
Here is my code.
OutputCode:#include <fstream.h> #include <iostream.h> #include <stdio.h> #include <windows.h> //definition for class student class Student { char name[10]; char grade; float marks; int rn; public : void getdata() { cout<<"\nenter name "; gets(name); cout<<"enter grade "; cin>>grade; cout<<"enter marks "; cin>>marks; cout<<"enter roll no "; cin>>rn; } void display() { cout<<"\nName ";puts(name); cout<<"Grade "<<grade; cout<<"\nMarks "<<marks; cout<<"\nroll no "<<rn; } }; int main() { Student arts[5]; fstream filin; //defining fstream objects filin.open("Stu.txt",ios::in|ios::out); //opening the file Stu.txt if(!filin) //if filin is zero { cout<<"\ncannot open file."; return 1; } cout<<"\nEnter details of students"; //Entering data for(int i=0;i<5;i++) { arts[i].getdata(); filin.write((char*)&arts[i],sizeof(arts[i])); //writing in Stu.txt } filin.close(); //closing file Stu.txt filin.open("Stu.txt",ios::in|ios::out); //opening file Stu.txt if(!filin) //checking again for filin { cout<<"\ncannot open file."; return 1; } filin.seekg(0); cout<<"\nthe contents of stu.txt are "; for(int i=0;i<5;i++) { filin.read((char*)&arts[i],sizeof(arts[i])); //reading from file arts[i].display(); } filin.close(); //closing file Stu.txt system("pause"); /*for pause progam as the program automatically exits which enables user to see results*/ }
enter name ay
enter grade A
enter marks 41
enter roll no 1
enter name ak
enter grade A
enter marks 42
enter roll no 2
enter name am
enter grade A
enter marks 43
enter roll no 3
enter name af
enter grade A
enter marks 44
enter roll no 4
enter name as
enter grade A
enter marks 45
enter roll no 5
the contents of stu.txt are
enter name ay
enter grade A
enter marks 41
enter roll no 1
enter name ak
enter grade A
enter marks 42
enter roll no 2
enter name am
enter grade A
enter marks 43
enter roll no 3
enter name af
enter grade
// here are the errors - no grade
enter marks 2.28266e-36
// garbage value
enter roll no 1627389952
// garbage value
enter name s
// missing a in name as
enter grade
// missing grade
enter marks 9.13139e-36
// garbage value
enter roll no 0
// garbage value



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.
@laserlight: but that code doesn't work in borland 5.0.2. it give errors. i have uploaded the image untitled.jpg 