Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- void name_file (std::string &input_string1, std::string &input_string2, std::string &output_string);
- void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file);
- void set_input_name (std::ifstream &input, std::string input_name);
- void set_output_name (std::ofstream &output, std::string output_name);
- void close_file_input(std::ifstream &close);
- void close_file_output(std::ofstream &close);
- int main ()
- {
- std::ifstream input_file1, input_file2;
- std::ofstream output_file;
- std::string input_filename1;
- std::string input_filename2;
- std::string output_filename;
- name_file(input_filename1, input_filename2, output_filename);
- set_output_name(output_file, output_filename);
- set_input_name(input_file1, input_filename1);
- set_input_name(input_file2, input_filename2);
- sort_input(input_file1, input_file2, output_file);
- close_file_input(input_file1);
- close_file_input(input_file2);
- close_file_output(output_file);
- std::cout << "The merger of the files is complete." << std::endl;
- return 0;
- }
- void name_file(std::string &input_string1, std::string &input_string2, std::string &output_string)
- {
- //name the ouput file
- std::cout << "Including the file extension, please enter the name of the desired output file: ";
- std::cin >> output_string;
- //name the first input file
- std::cout << "What is the name of the first input file? Please include the file extension: ";
- std::cin >> input_string1;
- //name the second input file
- std::cout << "What is the name of the second input file? Please include the file extension: ";
- std::cin >> input_string2;
- }
- void set_input_name (std::ifstream &input, std::string input_name)
- {
- //open file
- input.open(input_name.c_str());
- if (input.fail())
- {
- std::cout << "Error opening input file " << input_name << "." << std::endl;
- }
- }
- void set_output_name (std::ofstream &output, std::string output_name)
- {
- //open file
- output.open(output_name.c_str(), std::ios::app);
- if (output.fail())
- {
- std::cout << "Error opening output file " << output_name << "." << std::endl;
- }
- }
- void close_file_input(std::ifstream &close)
- {
- close.close();
- }
- void close_file_output(std::ofstream &close)
- {
- close.close();
- }
- void sort_input (std::ifstream &input_file1, std::ifstream &input_file2, std::ofstream &output_file)
- {
- //storage for input ints coming from two files
- int num1;
- int num2;
- char check1;
- char check2;
- //check to make sure first input is a number
- check1 = input_file1.peek();
- check2 = input_file2.peek();
- while(!(isdigit(check1)))
- {
- check1 = input_file1.get();
- check1 = input_file1.peek();
- }
- if(isdigit(check1))
- {
- input_file1 >> num1;
- }
- while(!(isdigit(check2)))
- {
- check2 = input_file2.get();
- check2 = input_file2.peek();
- }
- if(isdigit(check2))
- {
- input_file2 >> num2;
- }
- do
- {
- if(num2 < num1)
- {
- output_file << num2 << " " << std::endl;
- while(!(isdigit(check2)))
- {
- check2 = input_file2.get();
- check2 = input_file2.peek();
- }
- if(isdigit(check2))
- {
- input_file2 >> num2;
- }
- goto next;
- }
- if(num1 < num2)
- {
- output_file << num1 << " " << std::endl;
- while(!(isdigit(check1)))
- {
- check2 = input_file1.get();
- check2 = input_file1.peek();
- }
- if(isdigit(check1))
- {
- input_file1 >> num1;
- }
- goto next;
- }
- if(num1 == num2)
- {
- output_file << num1 << " " << num2 << " " << std::endl;
- while(!(isdigit(check1)))
- {
- check2 = input_file1.get();
- check2 = input_file1.peek();
- }
- if(isdigit(check1))
- {
- input_file1 >> num1;
- }
- while(!(isdigit(check2)))
- {
- check2 = input_file2.get();
- check2 = input_file2.peek();
- }
- if(isdigit(check2))
- {
- input_file2 >> num2;
- }
- }
- next:;
- }while ((!input_file1.eof()) && (!input_file2.eof()));
- if(input_file2.eof())
- {
- if(num2 < num1)
- {
- output_file << num2 << " " << std::endl;
- }
- else if (num1 == num2)
- {
- output_file << num1 << " " << num2 << " " << std::endl;
- }
- else
- {
- output_file << num1 << " " << std::endl;
- }
- while(!input_file1.eof())
- {
- check1 = input_file1.peek();
- if(isdigit(check1))
- {
- if(input_file1 >> num1)
- {
- output_file << num1 << " " << std::endl;
- }
- }
- else
- {
- check1 = input_file1.get();
- }
- }
- output_file << num1 << " " << std::endl;
- }
- if(input_file1.eof())
- {
- if(num1 < num2)
- {
- output_file << num1 << " " << std::endl;
- }
- else if (num1 == num2)
- {
- output_file << num1 << " " << num2 << " " << std::endl;
- }
- else
- {
- output_file << num2 << " " << std::endl;
- }
- while(!input_file2.eof())
- {
- check1 = input_file2.peek();
- if(isdigit(check2))
- {
- if(input_file2 >> num1)
- {
- output_file << num2 << " " << std::endl;
- }
- }
- else
- {
- check2 = input_file2.get();
- }
- }
- output_file << num2 << " " << std::endl;
- }
- /*if(input_file1.eof())
- {
- if(num1 < num2)
- {
- output_file << num1 << " " << std::endl;
- }
- else if (num1 == num2)
- {
- output_file << num1 << " " << num2 << " " << std::endl;
- }
- while(input_file2 >> num2)
- {
- output_file << num2 << " " << std::endl;
- }
- }
- */
- input_file1.clear();
- input_file2.clear();
- output_file.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement