jjgfjbfjf\

This commit is contained in:
Ваше Имя
2025-10-18 06:10:03 -04:00
parent c887022e65
commit 6d52da9bf9

72
wcl.c
View File

@@ -1,44 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
int count_lines(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "wcl: ошибка: не удалось открыть файл '%s'\n", filename);
return -1;
}
int lines = 0;
int ch;
int last_char = '\n';
while ((ch = fgetc(file)) != EOF) {
if (ch == '\n') {
lines++;
}
last_char = ch;
}
if (last_char != '\n' && last_char != EOF) {
lines++;
}
fclose(file);
return lines;
}
#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++) {
int lines = count_lines(argv[i]);
if (lines >= 0) {
printf("%d\t%s\n", lines, argv[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;
}