-
Notifications
You must be signed in to change notification settings - Fork 1
/
My copy command.c
52 lines (39 loc) · 917 Bytes
/
My copy command.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
//mycopy
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
FILE *fp, *wfp;
char buffer[BUFFER_SIZE];
int i;
if (argc < 3)
{
printf("Usage: <inputfile1> <inputfile2> ... <outputfile>\n");
return 1;
}
wfp = fopen(argv[argc - 1], "w");
if (wfp == NULL)
{
perror("Error opening output file");
return 1;
}
for (i = 1; i < argc - 1; i++)
{
fp = fopen(argv[i], "r");
if (fp == NULL)
{
perror("Error opening input file");
continue;
}
fprintf(wfp,"\n\nfile content are:%s\n",argv[i]);
while (fgets(buffer, BUFFER_SIZE, fp) != NULL)
{
fputs(buffer, wfp);
}
fclose(fp);
}
fclose(wfp);
printf("Files merged successfully.\n");
return 0;
}