Question:
c++ Consider two input files (with physical names “infile1.txt” and “infile2.txt”) where each input file co?
Taha
2012-04-25 08:11:17 UTC
Consider two input files (with physical names “infile1.txt” and “infile2.txt”) where each input
file contain in each line an integer value and the values in a file are already sorted in ascending
order. Write a program that reads the input files and merges their content into an output file (with
physical name “outfile.txt”) such that the values in the output file are also sorted in ascending
order. Your program should not crash in any of the files is empty.
Three answers:
?
2012-04-25 10:44:12 UTC
@oops. Naturally you are right. At the time, when I got the error message:

merge2.cpp: In function 'int main()':

merge2.cpp:11: error: 'merge' is not a member of 'std'

I did not have algorithm included in my code.

My apology.

I still like your solution very much.

----------------------------------------------------

hat down for oops. There was only one minor oops. Here the code:

#include

#include

#include

#include

using namespace std;

int main()

{

ifstream fin1("infile1.txt"), fin2("infile2.txt");

ofstream fout("outfile.txt");

istream_iterator ibegin1(fin1), ibegin2(fin2), iend;

ostream_iterator obegin(fout,"\n");

// there is no std::merge everything else is perfect. Thank you.

merge(ibegin1, iend, ibegin2, iend, obegin);

return 0;

}
oops
2012-04-25 08:55:21 UTC
std::ifstream fin1("infile1.txt"), fin2("infile2.txt");

std::ofstream fout("outfile.txt");

std::istream_iterator ibegin1(fin1), ibegin2(fin2), iend;

std::ostream_iterator obegin(fout,"\n");

std::merge(ibegin1, iend, ibegin2, iend, obegin);



@Michael:

What do you mean there's no std::merge? It's in the header.

http://en.cppreference.com/w/cpp/algorithm/merge



In fact, you're using it in your solution. You just happen to be writing 'using namespace std;' at the top, instead of qualifying each item explicitly like I am. I try not to encourage beginners to use 'using namespace std;' because it can cause conflicts that they might not be equipped to understand. For example, they might use the perfectly reasonable name 'count' for a variable and it will conflict with the 'count' function from the algorithm header.
Ari
2012-04-25 09:02:56 UTC
the best thing is to read both files into separate arrays (or vectors, because to use a vector, you don't need to know the initial size.). then sort the arrays at the same time.





#include

#include

#include

#include



using namespace std;



main() {



ifstream infile1("infile1.txt");

ifstream infile2("infile2.txt");



//error checking to see if the file opened.



vector file1;

vector file2;



int num;

while(infile1>>num) {

file1.push_back(num); //this adds the number to the end of the vector

}

infile1.close();

while (infile2>>num) {

file2.push_back(num);

}

infile2.close();



//error checking for empty vector. the answer would just be the other vector

if (file1.size()==0 && file2.size()==0) {

cerr<<"Nothing to do"<
infile1.close();

infile2.close();

exit(1);

}

if (file1.size()) {

for (int i=0; i
outfile<
}

outfile.close();

exit(1);

}

if (file2.size()==0) {

for (int i=0; i
outfile<
}

outfile.close();

exit(1);

}



ofstream outfile("outfile.txt");



int i=0, j=0;



while(i
if(file1[ i ] < file2[ j ]) {

outfile<
i++;

} else {

outfile<
j++:

}

}



while(i
outfile<
i++;

}

while(j
outfile<
j++;

}



outfile.close();

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...