Load custom class data from a file in C++
<p>I need to create a set of custom class objects from a text file in which the fields are saved one per line. I have the class</p>

<pre><code>class Person {
    private:
        string name, surname, address;
        /*...*/
    public:
        void load(istream&amp; in) {
            in &gt;&gt; nome &gt;&gt; cognome &gt;&gt; indirizzo;
    }
}
</code></pre>

<p>The class as also other fields such as phone numbers but I think this doesn't matter.
And I also redefined the <code>&gt;&gt;</code> operator for class <code>Person</code>:</p>

<pre><code>ifstream&amp; operator &gt;&gt; (ifstream &amp;in, Person&amp; p) {
    p.load(in);
    return in;
}
</code></pre>

<p>The file would look like</p>

<pre><code>Sherlock
Holmes
Baker st.
John
Smith
whatever st.
</code></pre>

<p>(it MUST be like this, no separators or empty lines, don't ask me why). I then loop through the file like this:</p>

<pre><code>Person p;
while (!(in &gt;&gt; p).eof()) {
    add(p);
};
</code></pre>

<p>The problem with my code is that after adding the last person, the loop is executed again and another Person is created with all fields set to "". I would like the loop to end after the last object is created. How could I achieve this? I'm sorry for my poor knowledge of English and C++.</p>
