Friday, May 21, 2010

C++ program to merge two files?

Suppose you are given two files sorted1.dat and sorted2.dat, which contain several sorted integer numbers.


Write a C++ program that reads from the two files and


merges the sorted numbers into a new file sortedAll.dat.





Example:





sorted1.dat contains:


1 3 5 6 7 9 11





sorted2.dat contains:


2 4 8 10 11 12 14 16





sortedAll.dat should contains:


1 2 3 4 5 6 7 8 9 10 11 11 12 14 16

C++ program to merge two files?
♠ I do not write in C++, but I’m an experienced programmer.


♣ If you still want this algorithm I will describe it in a relevant and readable way with necessary comments and details! you are welcum to click me!
Reply:alogrithm





store file one to array1


store file two to array2





sort(array1,array2,array3) store to array three





save array3 to file





easy algorithm, try not to expect code because you'll never learn if you don't do it yourself.





hint, if you don't know the size of the contents in each file, use dynamic arrays or dump them to a link list and sort the link list.
Reply:LOL I HATED C++, YOUR DOING COMP ENGINEERING??


OR CS MAJOR???





(DONT WORRY WE REALLY DONT USE TOO MUCH PROGRAMMING IF YOU GOING FOR COMP ENGINEERING)
Reply:Use the OS to do the work.





In windows, you can use system([DOS Command Here]) to sort the files, then you can merge the files.





Example





#include %26lt;stdlib%26gt;





int main()


{


system("SORT c:\\sorted1.dat");//sort your first file


system("SORT c:\\sorted2.dat");//sort your second file


system("Copy c:\\sorted1.dat+sorted2.dat sortAll.dat") //merge the files into the new file


system("SORT sortAll.dat");


}


No comments:

Post a Comment