Sie sind auf Seite 1von 2

#include "std_lib_facilities_3.

h" /* Program that prompts the user for two input files (file name and extension must be included, EX: input1.txt), then prompts the user to name an output file. The comparisons of the files will be in the output text document. Also, parts of this program was originially created by me (Kevin Bradshaw) last year, just modified and compiled correctly towards the specific exercise. */ struct Reading { string line1; Reading(string l1) :line1(l1){} }; struct Reading2 { string line2; Reading2(string l2) :line2(l2){} }; int main() try { cout << "Please enter an input file name and it's extension (EX - input1 .txt): "; string input1; getline(cin,input1); ifstream ist(input1.c_str()); if (!ist) error("can't open input file ",input1); vector<Reading>v1; string line1; while (getline(ist, line1)) { v1.push_back(Reading(line1)); } cout << "Please enter another input file name in the same format: "; string input2; getline(cin,input2); ifstream ist2(input2.c_str()); if (!ist2) error("can't open input file ",input2); vector<Reading2>v2; string line2; while (getline(ist2, line2)) { v2.push_back(Reading2(line2)); } cout << "Please enter name of output file in the same format: "; string file1; getline(cin,file1); ofstream ost(file1.c_str()); if (!ost) error("can't open output file ", file1); // Check to see which is the larger file so a range error doesn't occur.

int size1 = v1.size(); int size2 = v2.size(); int larger_file; if (v1.size() >= v2.size()) { larger_file = size2; } else { larger_file = size1; } // Vector lines are compared: int j = 0; for(j; j< larger_file; ++j) { ost << "\n" << j+1 << ':'; if (v1[j].line1 == v2[j].line2) ost << "OK \n"; else ost << "DIFF \n"; } cout << "DONE! Please open the created file in a seperate window!\n"; return 0; } catch(exception& e) { cerr << "Error: " << e.what() << endl; return 1; } catch(...) { cerr << "Unknown Error." << endl; return 2; }

Das könnte Ihnen auch gefallen