forked from 131/lab0.1_letscontinue
32 lines
789 B
C
32 lines
789 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");
|
|
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[]) {
|
|
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;
|
|
}
|
|
}
|