How to Join Two CSV Files Using a Unique Key

Objective:
to join two CSV files together into a third CSV file which are based on a unique key (which is in column 1 of CSV files 1 and 2).


Please ignore many of the "#include" items I am only displaying part of the code.
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;


int main()
{
	//Data repositories
	std::ifstream infileDX("2014 February.csv", std::ifstream::binary);//Dataset 1
	std::ifstream infileRd("", std::ifstream::binary);//Dataset 2 
	std::ofstream outfile("Master_Dataset.csv", std::ofstream::binary);
        //Combined data outcome of Dataset 1 and Dataset 3

	infileDX.seekg(0, infileDX.end);
	long size = infileDX.tellg();
	infileDX.seekg(0);

	char* buffer = new char[size];

	infileDX.read(buffer, size);
	outfile.write(buffer, size);
	delete[] buffer;
        return 0;
}


Basically all this does is read in the data and then writes the entire dataset over to the Master file. However, what I really need is to be able to match on the first column which is an "INT" and where those rows match be able to augment the Dataset 1 with Dataset 2 data and deposit all that data into a file named Master.

I wondered if it would ever back sense to create and if_copy() function. For example, if column 1 of Dataset 1 and column 1 of Dataset 2 match to push_back() the column data of Dataset 2 to Dataset 1 and saving it in the Master file.

Any help would be appreciated.
So what does the code you provided even have to do with this problem? If you want to find matching information then you're going to need to parse the file, not just copy the contents from one place to another. Since you're really only interested in the first element it should be fairly easy to accomplish your objectives. It'll be even easier if both files happen to be sorted on this "key" to begin with. I'd recommend reading one line at a time into a std::string then process the string with a stringstream to get your keys. If the keys match write the complete lines to the "Master" file.
Topic archived. No new replies allowed.