Comparing Two Text Files by Characters

closed account (9GEqko23)
Hello. I'm currently doing a C++ assignment and I was wondering why my code is not working. Sorry if the code looks sloppy.

What this code does is that it asks the user for two text files, then it outputs the text file and stores it into a char array. Then it compares the two text files in the "compare" function.

The problem with this code is that I input two identical text files yet it still says the files are different.

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;
}
You have several problems.
First one: do not loop on .eof() if you do not know how it works.

For example when reading last character from file it is consumed, but EOF bit is not set yet, so loop continues. Then you are trying to read one more character, read fails, EOF bit is set, but it is too late: you are incrementing num of characters anyway.

So loop on input operation instead. Example:
1
2
3
while ( compare1.get(filearr1[i1]); ) {
    count(i1, numchar1);
}


Second one is using strcmp for comparison of your arrays. There are two problems with it: the fact that your array is not null-terminated so strcmp will run out of bounds and will compare random memory and that it will fail to work properly if there will be null byte embedded in file. Just compare characters normally.

Bonus: C++11 style files comparison:
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
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>


int main()
{
    std::cout << "Enter  first file name: ";
    std::string first;
    std::getline(std::cin >> std::ws, first);
    std::cout << "Enter second file name: ";
    std::string second;
    std::getline(std::cin >> std::ws, second);

    std::ifstream f1 {first};
    std::ifstream f2 {second};

    if( not (f1 && f2)) {
        std::cerr << "Error opening files";
        return EXIT_FAILURE;
    }

    using raw_iter = std::istreambuf_iterator<char>;
    if(std::equal(raw_iter(f1), raw_iter(), raw_iter(f2), raw_iter())) {
        std::cout << "Files are identical\n";
    } else {
        std::cout << "files are different\n";
    }
}
In the while loops where you read the characters you store an extra character. On line 45/53: when the get(...) function detects an error/eof you increase the index nonetheless.

Change it to:
1
2
3
4
while (compare1 >> filearr1[numchar1])
	{
		++numchar1;
	}


In your compare(...) function:

Compare only if(num1 == num2) ... In all other cases the arrays are different.

Either you use the loop on line 103 or strcmp(...) on line 105. Not both.
In case of the loop: for (int i = 0; i < high; i++) // Note < not <=
In case of strcmp(...): Either append a '\0' or use strncmp(...).
But since you won't find the first different character, replace line 105: if(array1[i] == array2[i])
Remove line 107.
closed account (9GEqko23)
Got it to work. Thank you so much guys. Both of you were very helpful and very detailed in what mistakes I made and I highly appreciate it. This was my first post and I love this forum already. :)
Topic archived. No new replies allowed.