Advertisement
Lawnknome

lab4 with peek

Apr 22nd, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 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.     char check1;
  91.     char check2;
  92.  
  93.     //check to make sure first input is a number
  94.     check1 = input_file1.peek();
  95.     check2 = input_file2.peek();
  96.  
  97.     while(!(isdigit(check1)))
  98.     {
  99.         check1 = input_file1.get();
  100.         check1 = input_file1.peek();
  101.     }
  102.  
  103.     if(isdigit(check1))
  104.     {
  105.        input_file1 >> num1;
  106.     }
  107.  
  108.     while(!(isdigit(check2)))
  109.     {
  110.         check2 = input_file2.get();
  111.         check2 = input_file2.peek();
  112.     }
  113.  
  114.     if(isdigit(check2))
  115.     {
  116.         input_file2 >> num2;
  117.     }
  118.  
  119.     do
  120.     {
  121.         if(num2 < num1)
  122.         {
  123.             output_file << num2 << " " << std::endl;
  124.  
  125.             while(!(isdigit(check2)))
  126.             {
  127.                 check2 = input_file2.get();
  128.                 check2 = input_file2.peek();
  129.             }
  130.  
  131.             if(isdigit(check2))
  132.             {
  133.                 input_file2 >> num2;
  134.             }
  135.             goto next;
  136.         }
  137.  
  138.         if(num1 < num2)
  139.         {
  140.             output_file << num1 << " " << std::endl;
  141.  
  142.             while(!(isdigit(check1)))
  143.             {
  144.                 check2 = input_file1.get();
  145.                 check2 = input_file1.peek();
  146.             }
  147.  
  148.             if(isdigit(check1))
  149.             {
  150.                 input_file1 >> num1;
  151.             }
  152.             goto next;
  153.         }
  154.  
  155.         if(num1 == num2)
  156.         {
  157.             output_file << num1 << " " << num2 << " " << std::endl;
  158.  
  159.             while(!(isdigit(check1)))
  160.             {
  161.                 check2 = input_file1.get();
  162.                 check2 = input_file1.peek();
  163.             }
  164.  
  165.             if(isdigit(check1))
  166.             {
  167.                 input_file1 >> num1;
  168.             }
  169.  
  170.              while(!(isdigit(check2)))
  171.             {
  172.                 check2 = input_file2.get();
  173.                 check2 = input_file2.peek();
  174.             }
  175.  
  176.             if(isdigit(check2))
  177.             {
  178.                 input_file2 >> num2;
  179.             }
  180.         }
  181.  
  182.         next:;
  183.  
  184.     }while ((!input_file1.eof()) && (!input_file2.eof()));
  185.  
  186.     if(input_file2.eof())
  187.     {
  188.         if(num2 < num1)
  189.         {
  190.             output_file << num2 << " " << std::endl;
  191.         }
  192.  
  193.         else if (num1 == num2)
  194.         {
  195.             output_file << num1 << " " << num2 << " " << std::endl;
  196.         }
  197.  
  198.         else
  199.         {
  200.             output_file << num1 << " " << std::endl;
  201.         }
  202.  
  203.         while(!input_file1.eof())
  204.         {
  205.             check1 = input_file1.peek();
  206.  
  207.             if(isdigit(check1))
  208.             {
  209.                 if(input_file1 >> num1)
  210.                 {
  211.                     output_file << num1 << " " << std::endl;
  212.                 }
  213.             }
  214.  
  215.             else
  216.             {
  217.                 check1 = input_file1.get();
  218.             }
  219.         }
  220.  
  221.         output_file << num1 << " " << std::endl;
  222.     }
  223.  
  224.     if(input_file1.eof())
  225.     {
  226.         if(num1 < num2)
  227.         {
  228.             output_file << num1 << " " << std::endl;
  229.         }
  230.  
  231.         else if (num1 == num2)
  232.         {
  233.             output_file << num1 << " " << num2 << " " << std::endl;
  234.         }
  235.  
  236.         else
  237.         {
  238.             output_file << num2 << " " << std::endl;
  239.         }
  240.  
  241.         while(!input_file2.eof())
  242.         {
  243.             check1 = input_file2.peek();
  244.  
  245.             if(isdigit(check2))
  246.             {
  247.                 if(input_file2 >> num1)
  248.                 {
  249.                     output_file << num2 << " " << std::endl;
  250.                 }
  251.             }
  252.  
  253.             else
  254.             {
  255.                 check2 = input_file2.get();
  256.             }
  257.         }
  258.  
  259.         output_file << num2 << " " << std::endl;
  260.     }
  261.  
  262.     /*if(input_file1.eof())
  263.     {
  264.         if(num1 < num2)
  265.         {
  266.             output_file << num1 << " " << std::endl;
  267.         }
  268.  
  269.         else if (num1 == num2)
  270.         {
  271.             output_file << num1 << " " << num2 << " " << std::endl;
  272.         }
  273.  
  274.         while(input_file2 >> num2)
  275.         {
  276.             output_file << num2 << " " << std::endl;
  277.         }
  278.     }
  279.     */
  280.  
  281.     input_file1.clear();
  282.     input_file2.clear();
  283.     output_file.clear();
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement