-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandling24.c
77 lines (69 loc) · 1.58 KB
/
FileHandling24.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* C Program to Join Lines of Two given Files and
* Store them in a New file
*/
#include <stdio.h>
#include <stdlib.h>
/* Function Prototype */
int joinfiles(FILE *, FILE *, FILE *);
char ch;
int flag;
void main(int argc, char *argv[])
{
FILE *file1, *file2, *target;
file1 = fopen(argv[1], "r");
if (file1 == NULL)
{
perror("Error Occured!");
}
file2 = fopen(argv[2], "r");
if (file2 == NULL)
{
perror("Error Occured!");
}
target = fopen(argv[3], "a");
if (target == NULL)
{
perror("Error Occured!");
}
joinfiles(file1, file2, target); /* Calling Function */
if (flag == 1)
{
printf("The files have been successfully concatenated\n");
}
}
/* Code join the two given files line by line into a new file */
int joinfiles(FILE *file1, FILE *file2, FILE *target)
{
while ((fgetc(file1) != EOF) || (fgetc(file2) != EOF))
{
fseek(file1, -1, 1);
while ((ch = fgetc(file1)) != '\n')
{
if (ch == EOF)
{
break;
}
else
{
fputc(ch, target);
}
}
while ((ch = fgetc(file2)) != '\n')
{
if (ch == EOF)
{
break;
}
else
{
fputc(ch, target);
}
}
fputc('\n', target);
}
fclose(file1);
fclose(file2);
fclose(target);
return flag = 1;
}