Leitura de lines, words e bytes de stream
This commit is contained in:
parent
6ea69ee04e
commit
d4e47bf489
1 changed files with 22 additions and 1 deletions
23
wc.c
23
wc.c
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue