Leitura de lines, words e bytes de stream

This commit is contained in:
NRZ Code 2025-04-30 16:24:27 -03:00
parent 6ea69ee04e
commit d4e47bf489

23
wc.c
View file

@ -1,6 +1,27 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
int main() { int main(int argc, char **argv) {
char buffer[BUFSIZ];
int lines = 0, words = 0, bytes = 0;
FILE *stream = fopen(argv[1], "r");
if (stream == NULL) {
perror("Erro ao abrir o arquivo");
exit(EXIT_FAILURE);
}
while (fgets(buffer, BUFSIZ, stream) != NULL) {
/* bytes */
bytes += strlen(buffer);
char *str;
/* words */
str = strdup(buffer);
while (strtok_r(str, " \t\n", &str) != NULL)
words++;
/* lines */
lines++;
}
printf("%d %d %d %s\n", lines, words, bytes, argv[1]);
fclose(stream);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }