1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
void errorMessage();
void count(int& i, int& numchar);
int compare(char array1[], char array2[], int num1, int num2);
int main()
{
char file1[10], file2[10];
char filearr1[300], filearr2[300];
int i1 = 0, numchar1 = 0;
int i2 = 0, numchar2 = 0;
int sameOrDiff;
ifstream compare1, compare2;
cout << "Input the name of the first text file: ";
cin >> file1;
compare1.open(file1);
if (compare1.fail())
{
errorMessage();
return 0;
}
cout << "Input the name of the second text file: ";
cin >> file2;
compare2.open(file2);
if (compare2.fail())
{
errorMessage();
return 0;
}
while (!compare1.eof())
{
compare1.get(filearr1[i1]);
count(i1, numchar1);
}
compare1.close();
while (!compare2.eof())
{
compare2.get(filearr2[i2]);
count(i2, numchar2);
}
compare2.close();
for (int i = 0; i < numchar1; i++)
{
cout << filearr1[i];
}
for (int i = 0; i < numchar2; i++)
{
cout << filearr2[i];
}
sameOrDiff = compare(filearr1, filearr2, numchar1, numchar2);
return 0;
}
void count(int& i, int& numchar)
{
i++;
numchar++;
}
void errorMessage()
{
cout << "Error Opening File" << endl;
}
int compare(char array1[], char array2[], int num1, int num2)
{
int high;
if (num1 > num2)
{
high = num1;
}
else if (num1 < num2)
{
high = num2;
}
else
{
high = num1;
}
for (int i = 0; i <= high; i++)
{
int num = strcmp(array1, array2);
if (num == 0)
{
cout << array1[i];
}
else
{
cout << "Different" << endl;
cout << "First location of difference is at character location " << i << endl;
return 0;
}
}
return 1;
}
|