48 lines
1.1 KiB
C
48 lines
1.1 KiB
C
#include <stdio.h>
|
||
#include <ctype.h>
|
||
|
||
int main(int argc, char *argv[]) {
|
||
if (argc < 2) {
|
||
fprintf(stderr, "Использование: %s <файл1> [файл2 ...]\n", argv[0]);
|
||
return 1;
|
||
}
|
||
|
||
for (int i = 1; i < argc; i++) {
|
||
FILE *file = fopen(argv[i], "r");
|
||
if (file == NULL) {
|
||
fprintf(stderr, "Ошибка: не удалось открыть файл '%s'\n", argv[i]);
|
||
continue;
|
||
}
|
||
|
||
int lines = 0;
|
||
int words = 0;
|
||
int bytes = 0;
|
||
int ch;
|
||
int in_word = 0;
|
||
|
||
while ((ch = fgetc(file)) != EOF) {
|
||
bytes++;
|
||
if (ch == '\n') {
|
||
lines++;
|
||
}
|
||
if (isspace(ch)) {
|
||
if (in_word) {
|
||
words++;
|
||
in_word = 0;
|
||
}
|
||
} else {
|
||
in_word = 1;
|
||
}
|
||
}
|
||
|
||
if (in_word) {
|
||
words++;
|
||
}
|
||
|
||
fclose(file);
|
||
printf("%d\t%d\t%d\t%s\n", lines, words, bytes, argv[i]);
|
||
}
|
||
|
||
return 0;
|
||
}
|