Merging two .csv files based on data of one column

I'm new to C++ so please bear me.

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:

excel file 1:

person_id family_id job age
11 1 2 30
12 1 1 28
21 2 2 40
22 2 2 42
23 2 1 10
excel file 2:

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.

#include <iostream>;
#include <fstream>;
#include <sstream>;
#include <string>;
#include <iomanip>;
using namespace std;

ifstream personfile("PERSONS.csv");
ifstream mainfile("MAIN.csv");

int main(){
string line;
string linem;
string token;
string tokenm;
string tokenmm;
string person_id;
string household_id;
string row;
string sex;
string age;
string job;
string dl;

std::ofstream activity;

activity.open("Activity.txt", std::ios_base::app);

int i = 0;
int j = 0;

while (getline(personfile, line, '\n')){
istringstream iss(line);

while (getline(iss, token, ',')){
switch (i)
{
case 0 :
activity << token;
person_id = token;
break;
case 1 :
activity << ',' << token;
household_id = token;
break;
case 2 :
activity << ',' << token;
row = token;
break;
case 3 :
activity << ',' << token;
sex = token;
break;
case 4 :
activity << ',' << token;
age = token;
break;
case 5 :
activity << ',' << token;
job = token;
break;
case 6 :
activity << ',' << token;
dl = token;
while (getline(mainfile, linem, '\n')){
istringstream iss(linem);
while (getline(iss, tokenm, ',')){
if (tokenm == household_id){
activity << ',' << tokenm;

while (getline(iss, tokenmm, ',')){
activity << ',' << tokenmm;
}
activity << '\n';
}
}
}

break;
}

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);
}

thanks a lot kbw:)
Topic archived. No new replies allowed.