I am trying to read from a text file properties: char* and int and create objects of class B.Here is my implementation:
B * p=new B[3];
for(int i=0;i<3;i++)
{
p[i]=B("John",300);
}
ofstream file("example.txt");
for(int i=0;i<3;i++)
{
file<<p[i].getName()<<" "<<p[i].GetValue()<<endl;
}
file.close();
//saved succesfully
ifstream file2("example.txt");
B *q=new B[3];
char* temp;
int time1;
for(int i=0;i<3;i++)
{
file2>>temp>>time1;
char*name1=new char[strlen(temp)+1];
strcpy(name1,temp); //here name1 is red correctly
q[i]=B(name1,time1); //
}
file2.close();
The saving of the 3 objects is successful ,I have checked the file.However when I am reading from it 3 objects,the first object is correctly created ,but for the other objects the char* propery which I pass gets initialized as "," and the second parameter which is int is also correct.Why does the first object create successfully while the others don't?