Question #42045
I have to read contents from a data file to members of classes. What is the code to open and read data into the class members?
1
Expert's answer
2014-06-05T15:07:47-0400
#include <iostream>#include <fstream>#include <string>using namespace std;class Person {public:   Person() :      name(""),      surname(""),      age(0),      height(0),      weight(0),      phone("") {}   friend istream &operator>> (istream &in, Person &p);            friend ostream &operator<< ( ostream &out, Person &p );protected:   string name;   string surname;   int age;   int height;   int weight;   string phone;};istream &operator>> (istream &in, Person &p) {   in >> p.name >> p.surname >> p.age >> p.height >> p.weight >> p.phone;   return in;}ostream &operator<< ( ostream &out, Person &p ) {    return out << p.name << "\n"<< p.surname << "\n"<< p.age << "\n"<< p.height << "\n"<< p.weight<< "\n"<< p.phone;}int main() {   Person p;   ifstream in("person.txt");   in >> p;            cout << p << endl;   in.close();   return 0;}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS