1

I want to read 3 objects from file. For example, in main I wrote:

ifstream fis;
Person pp;
fis >> pp;
Person cc;
fis >> cc;
Person dd;
fis >> dd;

The problem is that for every Person, it reads me 3 objects. I need to create an array of objects?

ifstream& operator>>(ifstream&fis, Person &p){

    fis.open("fis.txt", ios::in);
    while (!fis.eof())
    {
        char temp[100];
        char* name;
        fis >> temp;
        name = new char[strlen(name) + 1];
        strcpy(name, temp);
        int age;
        fis >> age;


        Person p(name, age);
        cout << p;
    }

    fis.close();
    return fis;
}
8
  • " it reads me 3 objects" please add example that will show what you are thinking about Commented Jan 22, 2015 at 10:04
  • In file I have : Maria 12 John 13 Smith 12. I want to read for person pp just Maria 12. But for Person pp, it reads me all the names:Maria 12 John 13 Smith 12 Commented Jan 22, 2015 at 10:09
  • So remove while (!fis.eof()) Commented Jan 22, 2015 at 10:10
  • I try to remove while. Then for every object reads just Maria 12 Commented Jan 22, 2015 at 10:11
  • ifstream fis; Persoana pp; Persoana cc; Persoana dd; fis >> pp>>cc>>dd; Commented Jan 22, 2015 at 10:17

2 Answers 2

5

Problem:

You're opening and closing the input stream inside of your operator>>. So everytime it is executed it opens your file at the beginning, then reads to the end and closes the file again. At the next call it starts all over again.

Explaination:

The used input stream keeps track of it's current reading position. If you reinitialize the stream in every call, you reset the position in the file thus starting at the beginning again. If you're interested you can also check the position with std::ifstream::tellg().

Possible solution:

Prepare the input stream outside of operator>>, then just read one dataset per call. After all reading is done close the file on the outside.

Example:

Calling code:

#include<fstream>

// ... other code

std::ifstream fis;
Person pp, cc, dd;

fis.open("fis.txt", std::ios::in); // prepare input stream

fis >> pp;
fis >> cc;
fis >> dd;

fis.close(); // after reading is done: close input stream

operator>> code:

std::ifstream& operator>>(std::ifstream& fis, Person &p)
{
    // only read if everything's allright (e.g. eof not reached)
    if (fis.good())
    {
        // read to p ...
    }

    /*
     * return input stream (whith it's position pointer
     * right after the data just read, so you can start
     * from there at your next call)
     */
    return fis;
}
Sign up to request clarification or add additional context in comments.

1 Comment

as this is your first question: if my answer (or the answer of another user) solved your problem you are able to 'accept' it. You maybe want to take some time to also consider other proposed approaches before doing so.
2

Before you use operator>> you should prepare stream that you want to read from.

ifstream fis;
fis.open("fis.txt", ios::in);
Person pp;
fis >> pp;
Person cc;
fis >> cc;
Person dd;
fis >> dd;
fis.close();

Your code should look like this.

ifstream& operator>>(ifstream&fis, Person &p) {
    char temp[100];
    char* name;
    fis >> temp;
    name = new char[strlen(name) + 1];
    strcpy(name, temp);
    int age;
    fis >> age;
    Person p(name, age);
    cout << p;
    return fis;
}

Consider adding additional checking if stream is not empty and using strings instead of char*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.