-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1.c
73 lines (67 loc) · 1.87 KB
/
lab1.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
#include <stdio.h>
int main(int argc, char* argv[]) {
FILE* input_txt;
char* filename = argv[2];
char* option = argv[1];
if (fopen(filename, "r") == NULL) {
printf("Could not open the file");
return 1;
}
input_txt = fopen(filename, "r");
if ((strcmp(option, "-l") == 0) || (strcmp(option, "--lines") == 0)) {
int lines = 1;
int ch = fgetc(input_txt);
int cnt_chars = 0;
while (ch != EOF) {
cnt_chars++;
if (ch == '\n') { //windows
lines++;
}
ch = fgetc(input_txt);
}
if (cnt_chars == 0) {
fclose(input_txt);
printf("%d", 0);
return 0;
}
fclose(input_txt);
printf("%d", lines);
return 0;
}
if ((strcmp(option, "-c") == 0) || (strcmp(option, "--bytes") == 0)) {
int bytes = 0;
int ch = fgetc(input_txt);
while (ch != EOF) {
bytes++;
if (ch == '\n') { //windows
bytes++;
}
ch = fgetc(input_txt);
}
fclose(input_txt);
printf("%d", bytes);
return 0;
}
if ((strcmp(option, "-w") == 0) || (strcmp(option, "--words") == 0)) {
int words = 0;
int non_space = 0;
int ch = fgetc(input_txt);
while (ch != EOF) {
if (ch == '\n' || ch == ' ' || ch == '\t' || ch == '\f') {
if (non_space == 1) {
non_space = 0;
words++;
}
} else {
non_space = 1;
}
ch = fgetc(input_txt);
}
if (non_space) {
words++;
}
fclose(input_txt);
printf("%d", words);
return 0;
}
}