Classes, overloading >> to read object from file

Hello,
I am finding some difficulties overloading the >> operator to read from file. Here is the program(the part that needs help!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 // CLASS DEFITION
// in person.h
#define DELIM '|'
class Person
{
private:
 // DATA MEMBERS
  Name name;  // has strins(firstN, lastN) in that order
  Address address; // has strings(street, postalcode, town)in that order
  string persNr;
  int skoNr;

public:
  // CONTRUCTORS
  // Default constructor
  Person();
  // overloaded contructor
  Person(Name pName, Address pAddress, string pPersNr, int pSkoNr);
	
  // DESTRUCTOR
  ~Person();
  // MEMBER FUNCTIONS
  // setMember functions
  void setName(Name &pName);
  void setAddress(Address &pAddress);
  void setPersNr(string &pPersNr);
  void setSkoNr(int &pSkoNr);
  
  // get member functions
   Name getName() const;
   etc..
};
istream &operator >> (istream &is, Person &pers);  // to read from file
ostream &operator<<(ostream &os, const Person &pers); // to saveToFile

// in person.cpp
// i think this works 
ostream &operator<<(ostream &os, const Person &pers)
{
  os << pers.getName() << DELIM;
  os << pers.getAddress() << DELIM;
  os << pers.getPersNr() << DELIM;
  os << pers.getSkoNr();
  return os;
}

// My Problem
istream &operator>> (istream &is, Person &pers)
{
  string str;  // to read the strings
  Name newNam;
  Address newAddr;
  string persNr;
  int skoNr = 0;
  getline(is, str, DELIM); // evidently, getline doesn't read 
                           // anything than a string. How fix this?
                           // first string is Name object
 
return is;
}


I will appreciate any form of help. Thanks in advance.
Last edited on
How does your data file look like?
@gunnderfunner
my data file looks like this

First name|lastname|street name|postcode|socialsecurity number|height
// everything except height(int) is string. | is char as constant defined in header file


I have tried this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
istream &operator>> (istream &is, Person &pers)
{
	Name nam;
	string str;
	Address addr;
	string persNr;
	int skoNr = 0;

	getline(is, str, DELIM);
	nam.setFirstName(str);
	getline(is, str, DELIM);
	nam.setLastName(str);
	
	pers.setName(nam);
	
	getline(is, str, DELIM);
	addr.setStreet(str);
	getline(is, str, DELIM);
	addr.setPostNr(str);
	getline(is, str, DELIM);
	addr.setTown(str);

	pers.setAddress(addr);

	getline(is, str, DELIM);
	pers.setPersNr(persNr);
	cin >> skoNr;
	pers.setSkoNr(skoNr);
	cin.get();
	return is;
}


with aim of using this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void readPersFromFile(string filn)
{
	ifstream readFile(filn);  // file object that reads and opens a file
	if (readFile)  // if the file exist, read it.
	{
		vector<Person> readPersons;
		Person newPerson;
		while (readFile >> newPerson)    // THIS LINE CAUSES ALL MY HEADACE
			readPersons.push_back(newPerson);
		readFile.close();

		cout << " Persons from " << filn << endl;
		showPeople(readPersons);
	}
	else // file doesn't exist
		cout << " No file with the name " << filn << " exist in the directory!" << endl;
}


What am i missing?
SOLVED!
I figured out what i was missing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
istream &operator>> (istream &is, Person &pers)
{
	Name nam;
	string str;
	Address addr;
	string persNr;
	int skoNr = 0;

	getline(is, str, DELIM);
	nam.setFirstName(str);
	getline(is, str, DELIM);
	nam.setLastName(str);
	
// assign this to pers
	pers.setName(nam);
	
	getline(is, str, DELIM);
	addr.setStreet(str);
	getline(is, str, DELIM);
	addr.setPostNr(str);
	getline(is, str, DELIM);
	addr.setTown(str);
// assign this address to pers
	pers.setAddress(addr);

	getline(is, str, DELIM);
	pers.setPersNr(persNr);

       // TAKE OFF THESE LINES
	//cin >> skoNr;
	//pers.setSkoNr(skoNr);
	//cin.get();
         
         // REPLACE WITH THESE with #include <cstdlib> for atoi
         getline(is, str);
	int skonr = atoi(str.c_str()); // converts the string to int
	pers.setSkoNr(skonr);
	return is;
}


Now everything works like magic.
Thanks @lastchance. Your question
How does your data file look like?
was all i needed to fix this
Topic archived. No new replies allowed.