-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-difference-in-two-files.cpp
100 lines (78 loc) · 2.47 KB
/
set-difference-in-two-files.cpp
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
// This program reads integers from 2 files, creates an array of them and checks the set difference
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Declare variables
ifstream fileOne;
ifstream fileTwo;
int fileOneData;
int fileTwoData;
int fileOneCounter = 0;
int fileTwoCounter = 0;
int counter = 0;
// Open the two files
fileOne.open("file1.txt");
fileTwo.open("file2.txt");
// Check if file one opened successfully
if (fileOne)
{
// Run a loop to get the number of integers in the first file which will be used to create the firstArray
while (fileOne >> fileOneData) fileOneCounter++;
// Clear the buffer and return fileOne loop to the first position so we can read the data from the beginning
fileOne.clear();
fileOne.seekg(0, ios::beg);
// Create a new dynamic array for firstArray
int* firstArray = new int[fileOneCounter];
// Loop and get data from the file, then store in the newly created array
while (fileOne >> fileOneData) {
firstArray[counter] = fileOneData;
counter++;
}
counter = 0; // Reset counter to zero after the process, so that file two can also use it
// Check if file 2 opened successfully. Most of the comments in file one above applies here too
if (fileTwo)
{
while (fileTwo >> fileTwoData) fileTwoCounter++;
fileTwo.clear();
fileTwo.seekg(0, ios::beg);
int* secondArray = new int[fileTwoCounter];
while (fileTwo >> fileTwoData) {
secondArray[counter] = fileTwoData;
counter++;
}
cout << "Set difference: ";
// Loop through the first array.
// On each iteration, also loop through the second array to see if element in the first array exist in the second array
// If it exists, break out of the loop.
// If not, when it gets to the end, of the second loop, then output the data in the first loop as not being found in second array
// Then, we have the set difference
for (int i = 0; i < fileOneCounter; ++i)
{
int j;
for (j = 0; j < fileTwoCounter; ++j)
if (firstArray[i] == secondArray[j]) {
break;
}
if (j == fileTwoCounter) {
cout << firstArray[i] << " ";
}
}
cout << endl;
// Delete the dynamically created arrays (to free the space) as we are now done with them
delete[] firstArray;
delete[] secondArray;
}
else {
cout << "Could not open file 2";
}
}
else {
cout << "Could not open file 1";
}
// Close the two files that were opened
fileOne.close();
fileTwo.close();
return 0;
}