Advertisement
Lawnknome

lab4.cpp

Apr 22nd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. void name_file (std::string &input_string1, std::string &input_string2, std::string &output_string);
  6. void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file);
  7. void set_input_name (std::ifstream &input, std::string input_name);
  8. void set_output_name (std::ofstream &output, std::string output_name);
  9. void close_file_input(std::ifstream &close);
  10. void close_file_output(std::ofstream &close);
  11.  
  12. int main ()
  13. {
  14.     std::ifstream input_file1, input_file2;
  15.     std::ofstream output_file;
  16.  
  17.     std::string input_filename1;
  18.     std::string input_filename2;
  19.     std::string output_filename;
  20.  
  21.     name_file(input_filename1, input_filename2, output_filename);
  22.     set_output_name(output_file, output_filename);
  23.     set_input_name(input_file1, input_filename1);
  24.     set_input_name(input_file2, input_filename2);
  25.  
  26.     sort_input(input_file1, input_file2, output_file);
  27.  
  28.     close_file_input(input_file1);
  29.     close_file_input(input_file2);
  30.     close_file_output(output_file);
  31.  
  32.     std::cout << "The merger of the files is complete." << std::endl;
  33.  
  34.     return 0;
  35. }
  36.  
  37.  
  38. void name_file(std::string &input_string1, std::string &input_string2, std::string &output_string)
  39. {
  40.     //name the ouput file
  41.     std::cout << "Including the file extension, please enter the name of the desired output file: ";
  42.     std::cin >> output_string;
  43.  
  44.     //name the first input file
  45.     std::cout << "What is the name of the first input file?  Please include the file extension: ";
  46.     std::cin >> input_string1;
  47.  
  48.     //name the second input file
  49.     std::cout << "What is the name of the second input file?  Please include the file extension: ";
  50.     std::cin >> input_string2;
  51. }
  52.  
  53. void set_input_name (std::ifstream &input, std::string input_name)
  54. {
  55.     //open file
  56.     input.open(input_name.c_str());
  57.  
  58.     if (input.fail())
  59.     {
  60.         std::cout << "Error opening input file " << input_name << "." << std::endl;
  61.     }
  62. }
  63.  
  64. void set_output_name (std::ofstream &output, std::string output_name)
  65. {
  66.     //open file
  67.     output.open(output_name.c_str(), std::ios::app);
  68.  
  69.     if (output.fail())
  70.     {
  71.         std::cout << "Error opening output file " << output_name << "." << std::endl;
  72.     }
  73. }
  74.  
  75. void close_file_input(std::ifstream &close)
  76. {
  77.     close.close();
  78. }
  79.  
  80. void close_file_output(std::ofstream &close)
  81. {
  82.     close.close();
  83. }
  84.  
  85. void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file)
  86. {
  87.     //storage for input ints coming from two files
  88.     int num1;
  89.     int num2;
  90.  
  91.     //std::cout << "hello" << std::endl;
  92.  
  93.     input_file1 >> num1;
  94.     input_file2 >> num2;
  95.  
  96.     do
  97.     {
  98.         //std::cout << "do-while" << std::endl;
  99.         //used to check eof count for while loop.
  100.         //output_file << "file1 EOF: " << input_file1.eof() << std::endl;
  101.         //output_file << "file2 EOF: " << input_file2.eof() << std::endl;
  102.         if(num2 < num1)
  103.         {
  104.             output_file << num2 << " " << std::endl;
  105.             input_file2 >> num2;
  106.             goto next;
  107.         }
  108.  
  109.         if(num1 < num2)
  110.         {
  111.             output_file << num1 << " " << std::endl;
  112.             input_file1 >> num1;
  113.             goto next;
  114.         }
  115.  
  116.         if(num1 == num2)
  117.         {
  118.             output_file << num1 << " " << num2 << " " << std::endl;
  119.             input_file1 >> num1;
  120.             input_file2 >> num2;
  121.         }
  122.  
  123.         next:;
  124.  
  125.     }while ((!input_file1.eof()) && (!input_file2.eof()));
  126.  
  127.     //std::cout << "end" << std::endl;
  128.  
  129.     if(input_file2.eof())
  130.     {
  131.         if(num2 < num1)
  132.         {
  133.             output_file << num2 << " " << std::endl;
  134.         }
  135.  
  136.         else if (num1 == num2)
  137.         {
  138.             output_file << num1 << " " << num2 << " " << std::endl;
  139.         }
  140.  
  141.         while(input_file1 >> num1)
  142.         {
  143.             output_file << num1 << " " << std::endl;
  144.         }
  145.  
  146.     }
  147.  
  148.     if(input_file1.eof())
  149.     {
  150.         if(num1 < num2)
  151.         {
  152.             output_file << num1 << " " << std::endl;
  153.         }
  154.  
  155.         else if (num1 == num2)
  156.         {
  157.             output_file << num1 << " " << num2 << " " << std::endl;
  158.         }
  159.  
  160.         while(input_file2 >> num2)
  161.         {
  162.             output_file << num2 << " " << std::endl;
  163.         }
  164.     }
  165.  
  166.     input_file1.clear();
  167.     input_file2.clear();
  168.     output_file.clear();
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement