I have asked this question previously here and a similar question was closed.
SO based on a comment from another user, I have reframed my question:
In the first post, I was trying to read tha data from a file into an array with a struct.By using indata << p[i] and is >> p.fId, I was able to read values from data file into PersonId.
Now I want to try this:
struct PersonId
{
int fId;
};
struct PersonData
{
public:
typedef PersonData* Ptr;
PersonData();
PersonId fId;
istream& read(std::istream&);
};
istream& PersonData::read(std::istream& is)
{
is >> fId;
return is;
}
istream& operator >> (istream& is, PersonData &p)
{
// is >> p.fId;
return p.read(is);
}
int main ()
{
ifstream indata; // indata is like cin
int i;
indata.open("persons.txt", ios::in); // opens the file
if(!indata)
{ // file couldn't be opened
cout << "Error: file could not be opened" << endl;
exit(1);
}
int n = 5;
PersonData* p;
p = (PersonData*) malloc (n * sizeof(PersonData));
while ( !indata.eof() )
{
indata >> p[i];
i++;
}
for(i = 0; i < n; ++i)
{
cout << "PersonData [" << i << "] is " << p[i] << endl;
}
return 0;
}
I want to use member function "read" to actually read values into structures defined by PersonData. My question:
How to read the data from file into PersonId struct which is stored in the PersonData struct??
While reading PersonData[i], I should see it have a struct PersonId with updated value.
I hope my questions are clear now?