-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25_file_Cat.c
137 lines (120 loc) · 2.18 KB
/
25_file_Cat.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
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
/*****************************************************************************************
DOCUMENTATION:
NAME : Karthikeyan.V
DATE : 11.08.2021
DESCRIPTION : WAP to concatenate two files
OUTPUT :
./a.out file1.txt file2.txt file3.txt
DATA SUCCESSFULLY COPIED TO DESTINATION FILE
DATA SUCCESSFULLY COPIED TO DESTINATION FILE
./a.out file1.txt
karthi
END OF FILE REACHED
./a.out file1.txt file2.txt
karthi
karthi
./a.out
hello
hello
./a.out
hello this is karthikeyan
hello this is karthikeyan
******************************************************************************************/
#include<stdio.h>
void myfunc_cat(FILE *, FILE *);
int main(int argc, char **argv)
{
FILE *fptr1, *fptr2, *fptr3;
if(argc == 4)
{
fptr1 = fopen(argv[1], "r");
fptr2 = fopen(argv[2], "r");
if(fptr1 == NULL || fptr2 == NULL)
{
printf("ERROR : NO SUCH A FILE\n");
return 0;
}
else
{
fptr3 = fopen(argv[3], "w");
myfunc_cat(fptr3, fptr1);
myfunc_cat(fptr3, fptr2);
}
}
else if( argc == 3 )
{
fptr1 = fopen(argv[1], "r");
fptr2 = fopen(argv[2], "r");
if(fptr1 == NULL || fptr2 == NULL)
{
printf("ERROR : NO SUCH A FILE\n");
return 0;
}
char ch;
while(ch = fgetc(fptr1))
{
if(feof(fptr1)) //proceed till end of file
{
break;
}
putchar(ch);
}
while(ch = fgetc(fptr2))
{
if(feof(fptr2)) //proceed till end of file
{
break;
}
putchar(ch);
}
}
else if( argc == 2 )
{
fptr1 = fopen(argv[1], "r");
if(fptr1 == NULL)
{
printf("ERROR : NO SUCH A FILE\n");
return 0;
}
else
{
char ch;
while(ch = fgetc(fptr1))
{
if(feof(fptr1)) //proceed till end of file
{
printf("\nEND OF FILE REACHED\n");
break;
}
putchar(ch);
}
}
}
else if( argc == 1 )
{
char *str, ch;
while((ch = getchar()) != '\n')
{
putchar(ch);
}
printf("\n");
}
else
{
printf("\nINVALID ENTRY OF DATAS\n");
}
return 0;
}
void myfunc_cat( FILE *dest, FILE *src )
{
char ch;
while( ch = fgetc(src) )
{
if( feof(src) ) //proceed till end of file
{
printf("DATA SUCCESSFULLY COPIED TO DESTINATION FILE\n");
break;
}
fputc( ch, dest );
}
}