Advertisement
MutahirA_

Untitled

Nov 12th, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. void merge(ifstream& input1, ifstream& input2, ofstream& output);
  7. // Precondition:
  8. // The input files are text files.
  9. // The input files have been opened.
  10.  
  11.  
  12. int main()
  13. {
  14. ifstream infile1, infile2;
  15. ofstream outfile;
  16. char inName1[11], inName2[11], outName[12];
  17.  
  18. int next;
  19.  
  20. cout << "Enter the first of two input file names: " << endl;
  21. cin >> inName1;
  22.  
  23. cout << "Now a second input file name " << endl;
  24. cin >> inName2;
  25.  
  26. cout << "Enter the output file name. (WARNING: ANY EXISTING FILE WITH THIS NAME WILL) BE ERASED." << endl;
  27. cin >> outName;
  28.  
  29.  
  30. //closing files prior to reopening them for display
  31.  
  32. //must open file outName as an ifstream to read it.
  33. infile1.open(inName1);
  34. infile2.open(inName2);
  35. outfile.open(outName);
  36.  
  37. merge(infile1, infile2, outfile);
  38.  
  39.  
  40. //Output the content of inName1 in here
  41. cout << "Contents of file " << inName1 << " are: " << endl;
  42.  
  43. //Output the content of inName2 in here
  44. cout << "Contents of file " << inName2 << " are: " << endl;
  45.  
  46. //Output the content of outName in here
  47. cout << "Contents of merged file " << outName << " are:" << endl;
  48.  
  49. infile1.close();
  50. infile2.close();
  51. outfile.close();
  52. return 0;
  53.  
  54. }
  55.  
  56. void merge(ifstream& input1, ifstream& input2, ofstream& output)
  57. {
  58. int num1;
  59. int num2;
  60.  
  61. input1 >> num1;
  62. input2 >> num2;
  63.  
  64. while (!input1.eof() && !input2.eof()) {
  65. if (num1 < num2) {
  66. output << num1;
  67. input1 >> num1;
  68. }
  69.  
  70. else {
  71. output << num2;
  72. input2 >> num2;
  73. }
  74. }
  75. }
  76. // END The REAL merge
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement