mercredi 25 février 2015

rdbuf() strange behavior

I'm trying to merge two files into one by simply using rdbuf(), but when merged, the file's first line is missing. The code is below:



void mergeFiles(ifstream &file1, ifstream &file2, ofstream &file3) {

int num1;
int num2;

string firstLine_1;
string firstLine_2;

getline(file1, firstLine_1);
num1 = atoi(firstLine_1.c_str());

getline(file2, firstLine_2);
num2 = atoi(firstLine_2.c_str());

if (num1<num2) {
file3 << file1.rdbuf() << "\n" << file2.rdbuf();
} else {
file3 << file2.rdbuf() << "\n" << file1.rdbuf();
}
}

Aucun commentaire:

Enregistrer un commentaire