I have two .xlsx files one contains characteristics of each person e.g. person_id, family_id, age, job, etc. and the other file contains characteristics of each family which contains family_id, number of cars each family owns, etc.
I need to merge these two files in one file.
Both these two files have a column that contains family_id. The two files are like this:
family_id number of cars household size
1 2 2
2 1 3
I need to merge these to a file in which each row is representing one person characteristics and also his or her family characteristics like the following one:
person_id family_id job age number of cars household size
11 1 2 30 2 2
12 1 1 28 2 2
21 2 2 40 1 3
22 2 2 42 1 3
23 2 1 10 1 3
I've already saved two .xlsx files in .csv file and wrote the code below but it does not work properly.
i++;
}
i = 0;
j++;
}
personfile.close();
cout << tokenm << endl;
return 0;
}
How can I fix it? Sorry for my long question. I know C++ might not be the best for my purpose but unfortunately I cannot use any other programming languages.
I really haven't closely studied your code, but have you considered a vector of a structure/class to hold the data from the file? Read the first file, parse each line into the individual variables of the structure. Then read the second file, parse each line into individual variables, then search the vector to locate the "matching" record and then insert the proper values into that element, continue till done.
You're problems come from trying to re-read the family file; that whole area is problematic.
You need to read the file into a collection as you need to refer to it multiple times. Then you can read the Person file and process it in one pass.
A default container you might consider is vector. It'll need to hold records made up of "family_id number of cars household size". So you can define a record.
1 2 3 4 5 6
struct Family
{
int id;
int n_cars;
int household_size;
};
Then you can define the collection:
typedef std::vector<Family> Families;
And you'd read it with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Families ReadFamily(std::string filename)
{
Families families;
std::string line;
std::ifstream is(filename);
while (std::getline(is, line))
{
std::istringstream iss(line);
Family familiy;
iss >> family.id >> family.n_cars >> family.houshold_size;
families.push_back(family);
}
return std::move(families);
}