-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_args.c
More file actions
50 lines (40 loc) · 1.29 KB
/
cli_args.c
File metadata and controls
50 lines (40 loc) · 1.29 KB
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
// command_line_args.c
// Using command-line arguments - making programs flexible and useful
#include <stdio.h>
#include <stdlib.h> // for exit()
// Simple file copy program that takes input and output filenames from command line
int main(int argc, char *argv[]) {
FILE *in_fp, *out_fp;
int c;
// argc = argument count, argv = argument vector (array of strings)
printf("argc = %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
// Check correct usage
if (argc != 3) {
fprintf(stderr, "Usage: %s <input_file> <output_file>\n", argv[0]);
return 1;
}
// Open input file for reading
in_fp = fopen(argv[1], "r");
if (in_fp == NULL) {
perror("Error opening input file");
return 1;
}
// Open output file for writing
out_fp = fopen(argv[2], "w");
if (out_fp == NULL) {
perror("Error opening output file");
fclose(in_fp); // clean up already opened file
return 1;
}
// Copy character by character (building on your copy.c and file_io-01.c)
while ((c = fgetc(in_fp)) != EOF) {
fputc(c, out_fp);
}
fclose(in_fp);
fclose(out_fp);
printf("File copied successfully: %s -> %s\n", argv[1], argv[2]);
return 0;
}