forked from 131/lab0.1_letscontinue
11
This commit is contained in:
36
wcl.c
Normal file
36
wcl.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user