-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.16.3.c
57 lines (42 loc) · 964 Bytes
/
ex.16.3.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
// Program to copy one file to another
#include <stdio.h>
#include <ctype.h>
void chomp (char* const s, int max_len)
{
for ( int i = 0; i < max_len; i++ ) {
if ( s[i] == '\n' ) {
s[i] = '\0';
break;
}
}
}
int main (void)
{
char inName[64], outName[64];
FILE *in, *out;
int c;
// get file names from use
printf ("Enter name of file to be copied: ");
fgets (inName, 64, stdin);
chomp (inName, 64);
printf ("Enter name of output file: ");
fgets (outName, 64, stdin);
chomp (outName, 64);
// open input and output files
if ( (in = fopen (inName, "r")) == NULL ) {
printf ("Can't open %s for reading.\n", inName);
return 1;
}
if ( (out = fopen (outName, "w")) == NULL ) {
printf ("Can't open %s for writing.\n", outName);
return 2;
}
// copy in to out
while ( (c = getc (in)) != EOF )
putc (toupper (c), out);
// Close open files
fclose (in);
fclose (out);
printf ("File has been copied.\n");
return 0;
}