4
I’m trying to make a little program in C that opens a file .txt
, .c
or any other in read mode, to count the comments made with //
or /* */
.
I’m doing it the following way:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//funcao de contagem de comentarios
void contalinha(FILE *pFile,int *coment1L, int *comentVL)
{
char buff_linha[100];
char atual, prox;
int i;
while (!feof(pFile))
{ //DO
fgets(buff_linha, 100, pFile);
puts(buff_linha);//PROVA QUE O BUFF ESTA FUNCIONANDO
for(i=0; i<= 100; i++)//laco lendo o buff da linha
{
atual = buff_linha[i];
prox = buff_linha[i+1];
if(atual == '/' && prox == '/'){
*coment1L += 1;// comentarios de uma linha
}
else
if(atual == '/' && prox == '*'){
*comentVL += 1;// comentarios de varias linhas
}
}
}
return;
}
int main (int argc, char *argv[])
{
int n = 0;
int coment1L = 0, comentVL = 0, flag = 0, flag2 = 0;
FILE *pFile;
system("cls");
pFile = fopen (argv[1], "r");
if (pFile !=NULL)
{
printf("Lendo arquivo...\n\n");
contalinha(pFile,&coment1L, &comentVL);
fclose(pFile);
if(strcmp(argv[2], "-c") || strcmp(argv[2], "-comment")){
printf("\n\t\t - Contagem de Comentarios - \n\n");
printf("\n\tnumero de comentarios: %d", coment1L + comentVL);
printf("\n\tNumero de comentarios com //: %d", coment1L);
printf("\n\tNumero de comentarios com /*: %d", comentVL);
printf("\n\n");
}
}
system("pause");
return 0;
}
I’ll use it to read codes .c
. I’m reading it line by line from the file,
storing the line in a vector buff[100]
and traversing this vector in search of the //
or /*
. Until the part of storing in the buff
is working, but it seems to me that you are not going through the vector looking for matches.
Hello, Luiz Claudio. Welcome. Could you explain better what the symptoms are? How do you know that it is not working?
– Pablo Almeida
I’m running and he’s playing numbers without logic as a result
– Luiz Claudio