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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
| /**********************************************************************
Название файла: f1.bin f2.bin f3.bin
-----------------------------------------------------------------------
Содержание файла (v1): abc pro abc
def def
pro
-----------------------------------------------------------------------
Содержание файла (v2): abc abc
-----------------------------------------------------------------------
Содержание файла (v3): abc bcd abc
bcd bcd
eef eef
-----------------------------------------------------------------------
Содержание файла (v4):
**********************************************************************/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int VALUE_SIZE = 30;
class Struktura {
private:
char value[VALUE_SIZE + 1];
public:
Struktura(const char *v = "");
void setValue(const char *v);
void fillObject();
void write(ostream &fout);
bool read(istream &fin);
void print();
};
int main() {
ofstream fout1;
ofstream fout2;
ofstream fout3;
ifstream fin1;
ifstream fin2;
ifstream fin3;
Struktura s;
fout1.open("f1.bin", ios::binary);
cout << "Enter the contents f1.bin:" << endl;
s.fillObject();
while (!cin.eof()) {
s.write(fout1);
s.fillObject();
};
fout1.close();
cin.clear();
fout2.open("f2.bin", ios::binary);
cout << "Enter the contents f2.bin:" << endl;
s.fillObject();
while (!cin.eof()) {
s.write(fout2);
s.fillObject();
};
fout2.close();
cin.clear();
cout << "File f1 components:" << endl;
fin1.open("f1.bin", ios::binary);
while (s.read(fin1)) s.print();
fin1.close();
cout << "File f2 components:" << endl;
fin2.open("f2.bin", ios::binary);
while (s.read(fin2)) s.print();
fin2.close();
char buffer[36];
int a = 0, b = 0;
char i, k;
fin1.open("f1.bin", ios::binary);
fin2.open("f2.bin", ios::binary);
fout3.open("f3.bin", ios::binary);
fin1.seekg(0, fin1.end);
int filesize1 = fin1.tellg();
filesize1 = filesize1 / 36;
fin2.seekg(0, fin1.end);
int filesize2 = fin2.tellg();
filesize2 = filesize2 / 36;
fin1.seekg(0, ios::beg);
fin2.seekg(0, ios::beg);
fin1.read((char*)&i, sizeof(char));
fin2.read((char*)&k, sizeof(char));
while (fin1&&fin2) {
if (k > i) {
fin1.seekg(36 * a);
fin1.read(buffer, 36);
fout3.write(buffer, 36);
a++;
fin1.read((char*)&i, sizeof(char));
}
if (i > k) {
fin2.seekg(36 * b);
fin2.read(buffer, 36);
fout3.write(buffer, 36);
b++;
fin2.read((char*)&k, sizeof(char));
}
else if (i == k) {
fin1.seekg(36 * a);
fin1.read(buffer, 36);
fout3.write(buffer, 36);
a++;
b++;
fin1.seekg(36 * a);
fin2.seekg(36 * b);
fin1.read((char*)&i, sizeof(char));
fin2.read((char*)&k, sizeof(char));
};
};
if (!fin1) {
fin2.seekg(36 * b);
for (int i = 0; i < filesize2 - b; i++) {
fin2.read(buffer, 36);
fout3.write(buffer, 36);
};
};
if (!fin2) {
fin1.seekg(36 * a);
for (int i = 0; i < filesize1 - a; i++) {
fin1.read(buffer, 36);
fout3.write(buffer, 36);
};
};
fin1.close();
fin2.close();
fout3.close();
cout << "File f3 components:" << endl;
fin3.open("f3.bin", ios::binary);
while (s.read(fin3)) s.print();
fin3.close();
return 0;
};
Struktura::Struktura(const char *v) {
setValue(v);
};
void Struktura::setValue(const char *v) {
strncpy(value, v, VALUE_SIZE);
value[VALUE_SIZE] = '\0';
};
void Struktura::fillObject() {
char vv[VALUE_SIZE + 1];
cout << "To end input press: CTRL+Z." << endl;
cout << "Enter a value and press Enter." << endl;
cin >> vv; setValue(vv);
}
void Struktura::write(ostream &fout) {
fout.write((char*)this, sizeof(Struktura));
};
bool Struktura::read(istream &fin) {
fin.read((char*)this, sizeof(Struktura));
return fin.good();
};
void Struktura::print() {
cout << value << endl;
}; |