#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
FILE *entrada;
int c, c_antes;
unsigned long ncar, npal, nlin;
/* Ha' argumentos suficientes? */
if(argc != 2)
{
fprintf(stderr, "Tem de passar um argumento!\n");
return EXIT_FAILURE;
}
if((entrada = fopen(argv[1], "r")) == 0)
{
fprintf(stderr, "Erro abrindo o ficheiro \"%s\".\n", argv[1]);
return EXIT_FAILURE;
}
ncar = npal = nlin = 0;
c_antes = '\n';
while((c = getc(entrada)) != EOF)
{
ncar++;
if(c == '\n')
nlin++;
if(isspace(c) && !isspace(c_antes))
npal++;
c_antes = c;
}
if(!isspace(c_antes))
npal++;
if(c_antes != '\n')
nlin++;
printf("Ficheiro \"%s\":\n", argv[1]);
printf("Linhas: %lu\n", nlin);
printf("Palavras: %lu\n", npal);
printf("Caracteres: %lu\n", ncar);
fclose(entrada);
return EXIT_SUCCESS;
}