Search code examples
c++serializationfilestreams

Reading data from a text file with different variable types in C/C++


I'm building a simple family tree using classes, where each person has an ID and can have one or more relationships, and each relationship also has an ID. I'm trying to import this file in order to be able to use the class methods to import a tree:

9
Charless 
0 
F
2 5
Dianaaaa 
0 
M
1
William 
1 
M
6 
Harry 
1 
M
-1 
... 

For some context, the text file shows the number of people in the tree, name, son of relationship number X, gender, and ID of the person this person has a relationship with. If the person doesn't have any relationships, "-1" is shown. I'm incrementing the IDs automatically as I add a person or a relationship through the class methods. To import the file, I'm doing something like this:

ifstream f2;
f2.open("databasev2.txt");
string name; char gender;
int personrelationshipid; int sonofrelationshipid;
int numberOfPeople;
vector<int>relationships;
f2 >> numberOfPeople;
if (f2.is_open())
{ 
while (f2 >> name >> sonofrelationshipid >> gender )
{

    while (f2 >> personrelationshipid)
    {
        relationships.push_back(personrelationshipid);
        f2.ignore(0, ' '); 
    }
//...do something with the variables

My current problem is that the loop is stopping after the first iteration. I'm not sure if it's considering that the second name "Dianaaa" is no longer a string... Right now, it is reading "9", "Charless", "0", "F", "2" and "5", and inserting them into the vector, and then it stops. If I only have one relationship, this doesn't happen (i.e. if I remove the 5)

Moreover, I would like to add names with spaces between them - to do this, I think I just need to create a string and use f2.getline(name,string), then clear the buffer and remove the newline character so I don't have trouble reading the next line, am I right?

I can't use boost/JSON to serialize the information - I have to do this manually, so I would appreciate some help in "reinventing the wheel". However, I can edit the file as I want, adding some delimiters.

Thanks in advance


Solution

  • You need to call f2.clear() every time a loop quits, because you use the stream as a boolean operand. Therefore when the loop quits, there are error flags which cause subsequent stream operations to fail.