-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit_test.c
66 lines (53 loc) · 1.34 KB
/
split_test.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#ifdef CUSTOM
#include "my_stdio.h"
#endif
int main(int argc, char *argv[]) {
FILE *f1, *f2;
if (argc != 4) {
printf("usage: ./split_test[_custom | _std] src-filename dest-filename split-option\n");
printf("\t split-option is either\n");
printf("\t\t 1: char by char\n");
printf("\t\t 2: word by word \n");
exit(1);
}
if (!strcmp(argv[1], argv[2])) {
printf("The src-filename is the same as the dest-filename\n");
exit(2);
}
f1 = fopen(argv[1], "r");
if (f1 == NULL) {
printf("Can not open src-filename\n");
exit(3);
}
f2 = fopen(argv[2], "w");
if (f2 == NULL) {
printf("Can not open dest-filename\n");
exit(4);
}
fprintf(f2, "Input file: %s\n", argv[1]);
int param = atoi(argv[3]);
if (param == 1) {
char c;
if (!fscanf(f1, "%c", &c)) {};
while (!feof(f1))
{
fprintf(f2, "Character %c read, its ASCII code is %d\n", c, c);
if (!fscanf(f1, "%c", &c)) break;
}
} else if (param == 2) {
char s[22];
while (!feof(f1))
{
if (!fscanf(f1, "%s", s)) break;
fprintf(f2, "Word %s read, its length is %d\n", s, (int) strlen(s));
}
} else {
exit(5);
}
fclose(f1);
fclose(f2);
return 0;
}