first
This commit is contained in:
45
wcl.c
Normal file
45
wcl.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user