根据一列

时间:2016-02-04 06:31:26

标签: c++ csv merge

我是C ++的新手,所以请耐心等待。

我有两个.xlsx文件,其中一个包含每个人的特征,例如person_idfamily_idagejob等,其他文件包含每个系列的特征,每个系列包含family_idnumber of cars每个系列拥有等等。

我需要将这两个文件合并到一个文件中。

这两个文件都有一个包含family_id的列。这两个文件是这样的:

excel文件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文件2:

  family_id      number of cars    household size
     1                  2                 2
     2                  1                 3

我需要将这些合并到一个文件中,其中每一行代表一个人的特征,以及他或她的家庭特征,如下所示:

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

我已经在.csv文件中保存了两个.xlsx文件并编写了下面的代码,但它无法正常工作。

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

我该如何解决?对不起我的长话题。我知道C ++可能不是最适合我的目的,但不幸的是我不能使用任何其他编程语言。

0 个答案:

没有答案
相关问题