Files
lab0.1_letscontinue/wcl.c
etrushko05 e5870253c4 11
2025-10-18 04:23:04 -04:00

37 lines
893 B
C

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void process_file(const char *filename, long *lines, long *words, long *bytes) {
FILE *file = fopen(filename, "r");
if (!file) {
perror(filename);
return;
}
int ch;
int in_word = 0;
while ((ch = fgetc(file)) != EOF) {
(*bytes)++;
if (ch == '\n') (*lines)++;
if (isspace(ch)) {
in_word = 0;
}
else if (!in_word) {
in_word = 1;
(*words)++;
}
}
fclose(file);
}
int main(int argc, char *argv[]) {
long total_lines = 0, total_words = 0, total_bytes = 0;
printf("Name\tLines\tBytes\tWords\n");
for (int i = 1; i < argc; i++) {
long lines = 0, words = 0, bytes = 0;
process_file(argv[i], &lines, &words, &bytes);
printf("%s\t%ld\t%ld\t%ld\n",argv[i], lines, bytes, words);
total_lines += lines;
total_words += words;
total_bytes += bytes;
}
}