Files
lab1_git/wcl.c
Ваше Имя 6d52da9bf9 jjgfjbfjf\
2025-10-18 06:10:03 -04:00

55 lines
1.4 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> // для isspace()
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, "wcl: ошибка: не удалось открыть файл '%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;
}