-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmerge_files.c
62 lines (58 loc) · 1.35 KB
/
merge_files.c
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
//program to merge two files
#include <stdio.h>
#include <stdlib.h>
struct record
{
char name[20];
int roll;
int marks;
} stu1, stu2;
int main(void)
{
FILE *fp1, *fp2, *fp3;
int i, j;
if ((fp1 = fopen("SelectionA", "rb")) == NULL)
{
printf("Error in openning file..\n");
exit(1);
}
if ((fp2 = fopen("SelectionB", "rb")) == NULL)
{
printf("Error in openning file..\n");
exit(1);
}
if ((fp3 = fopen("merged", "wb")) == NULL)
{
printf("Error in openning file..\n");
exit(1);
}
i = fread(&stu1, sizeof(stu1), 1, fp1);
j = fread(&stu2, sizeof(stu2), 1, fp2);
while ((i == 1) && (j == 1))
{
if (stu1.marks > stu2.marks)
{
fwrite(&stu1, sizeof(stu1), 1, fp3);
i = fread(&stu1, sizeof(stu1), 1, fp1);
}
else
{
fwrite(&stu2, sizeof(stu2), 1, fp3);
j = fread(&stu2, sizeof(stu2), 1, fp2);
}
}
while (i == 1)
{
fwrite(&stu1, sizeof(stu1), 1, fp3);
i = fread(&stu2, sizeof(stu2), 1, fp1);
}
while (j == 1)
{
fwrite(&stu1, sizeof(stu1), 1, fp3);
j = fread(&stu2, sizeof(stu2), 1, fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}