Use como quiser, mas não me encha o saco se não funcionar :P
#include<stdio.h>
#include<stdlib.h>
#include< string.h>
int
main(int argc, const char *argv[])
{
int i, contchar = 0, contline = 0, contword = 0, letra = 0,
nonletra = 0;
int totalchar = 0, totalline = 0, totalword = 0;
int showchar = 0, showline = 0, showword = 0, errof = 1;
char c;
FILE *arq;
/*
* Decide pelo numero de argumentos o fluxo de programa. Caso nenhum
* argumento seja fornecido recebe dados do stdin, caso contrario o
* programa processara argumentos ou arquivos
*/
if (argc <= 1) {
contchar = 0;
contline = 0;
contword = 0;
while ((c = getc(stdin)) != EOF) {
if (c >= '!' && c <= '~') {
letra = 1;
nonletra = 0;
} else {
nonletra = 1;
if (letra) {
contword++;
}
if (c == '\n')
contline++;
letra = 0;
}
contchar++;
}
printf("\t %d \t %d \t %d \n", contline, contword, contchar);
} else {
for (i = 1; i < argc; i++) {
/* Verifica os argumentos passados */
if (!strcmp(argv[i], "-l")) {
showline = 1;
errof = 0;
}
if (!strcmp(argv[i], "-w")) {
showword = 1;
errof = 0;
}
if (!strcmp(argv[i], "-c")) {
showchar = 1;
errof = 0;
}
if ((arq = fopen(argv[i], "r")) == NULL) {
if (errof == 1)
printf("Erro na abertura do arquivo de entrada: %s \n", argv[i]);
errof = 1;
} else {
arq = fopen(argv[i], "r");
/* Loop principal */
contchar = 0;
contline = 0;
contword = 0;
while ((c = getc(arq)) != EOF) {
if (c >= '!' && c <= '~') {
letra = 1;
nonletra = 0;
} else {
nonletra = 1;
if (letra) {
contword++;
}
if (c == '\n')
contline++;
letra = 0;
}
contchar++;
}
if (showline + showword + showchar != 0) {
if (showline)
printf("\t %d", contline);
if (showword)
printf("\t %d", contword);
if (showchar)
printf("\t %d", contchar);
printf("\t %s \n", argv[i]);
} else {
printf("\t %d \t %d \t %d \t %s \n", contline, contword, contchar, argv[i]);
}
totalchar += contchar;
totalword += contword;
totalline += contline;
fclose(arq);
}
}
/*
* Decide ou nao se vai exibir total no fim do processamento
* de acordo com o numero e validade dos argumentos passados
* ao programa.
*/
if (showline)
argc--;
if (showword)
argc--;
if (showchar)
argc--;
if (argc > 2) {
if (showline + showword + showchar != 0) {
if (showline)
printf("\t %d", totalline);
if (showword)
printf("\t %d", totalword);
if (showchar)
printf("\t %d", totalchar);
printf(" Total \n");
} else {
printf("\t %d \t %d \t %d \t Total \n", totalline, totalword, totalchar);
}
}
}
return 0;
}
--
See Ya
roses are #FF0000
violets are #0000FF
all my base
are belong to you
http://xsl4v3.metareciclagem.org
Jabber: [EMAIL PROTECTED]
http://www.flickr.com/photos/slave/
_______________________________________________ Lista de discussão da MetaReciclagem Envie mensagens para [email protected] http://lista.metareciclagem.org
