Reading from file - Overloaded operator problem

Hi!

I have a program that's supposed to store people in a database. My program is working so far, but I have a problem reading from file after I save it to .txt.

Any ideas?

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
  const char DELIM = '|';

///////////////////////////////////////////////////////////////////////////
/// readFromFile //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void PersonList::readFromFile()
{
    // "filename" is a datamember of a class
    std::ifstream inFile("../../_Resources/" + filename + ".txt", std::ios::in);

// Clear the list of persons from any previously added persons.
    personDatabase.clear();
    Person tmpPerson;
//Create a temporary object of type Person in order to read persons from the list while new lines still exist.
    while (inFile >> tmpPerson)
    {
// Add person to list.
        personDatabase.push_back(tmpPerson);
    }
// Closing the file.
    inFile.close();
}
///////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////
/// writeToFile ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
void PersonList::writeToFile()
{
    // Open the file to write to.
   std::ofstream outFile("../../_Resources/" + filename + ".txt", std::ios::out);

// Write each person in the list on a separate line.
    for (auto& person : personDatabase) // For each object of type Person in vector
    {
        outFile << person << std::endl;
    }
// Close the file.
   outFile.close();
}
/////////////////////////////////////////////////////////////////////////// 


I try accessing the PersonList::writeToFile() by the following function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void UserInterface::load()
{
    std::string fileName;
// Displaying header for method to the user.
    std::cout << "Loading list from file" << std::endl;


// Requesting file path from the user.
    std::cout << "\nInput name of the file to load the list from: ";
    getline(std::cin, fileName);


// Setting file path for the personlist.
    personList.setFilename(fileName);


// Loading list from file.
    personList.readFromFile();


// Displaying success message to the user.
    std::cout << "List have been loaded from " << fileName << std::endl;
}


The overloaded operator for person class:

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

const char DELIM = '|';

std::istream &operator>>(std::istream &is, Person &person)
{
    std::string fName, lName, streetAddress, postNum, city, persNr;
    int skoNr;

    getline(is,fName, DELIM);
    getline(is, lName, DELIM);
    person.setName(Name(fName,lName));


    getline(is, streetAddress, DELIM);
    getline(is, postNum, DELIM);
    getline(is, city, DELIM);
    person.setAddress(Address(streetAddress, postNum, city));

    getline(is, persNr, DELIM);
    person.setPersNr(persNr);

    is >> skoNr;
    is.get();

    person.setSkoNr(skoNr);

    return is;
}


Can you see an obvious problem here or do I need to submit more code? I have like 25 .cpp and .h files, so hopefully someone can spot the problem from here!
When you open a file in writeToFile()/readFromFile() you should always check whether the file could be opened (e.g. is_open()).

Please post the operator<<(...)
Hi! Okay, Ill check.


<<:
1
2
3
4
5
6
7
8
9
std::ostream &operator<<(std::ostream &os, const Person &person)
{
    os << person.getName() << DELIM;
    os << person.getAddress() << DELIM;
    os << person.getPersNr() << DELIM;
    os << person.getSkoNr();

    return os;
}
Nvm, I fcked up.
Forgot to change << operator for other classes! Now it's working!
The operator<<(...) does not match the operator>>(...). I suggest that you make it so that it match.

Note that there is not delimiter after os << person.getSkoNr();. So it will most likely fail when reading a second data set.
Topic archived. No new replies allowed.