Comments count / / E / * * / in C

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 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 will use it to read .c codes. I am reading line by line from the file, storing the line in a vector buff[100] and traversing that vector in search of the // or /*. Even the store part in buff is working, but it seems to me that it is not traversing the vector looking for matches.

 4
Author: Edilson, 2015-11-29

1 answers

Wave.

I recommend that you exchange fgets for fscanf;

int line_size = fscanf( pFile, "%s", &buff_linha);
for(int i = 0 ; i < line_size ; i++ )
{
   // realiza a contagem
}

Another approach would be to make the call from strlen

int line_size = strlen(buff_linha);

Most likely you are reading garbage when reading the entire vector and not just reading the string down to the character '\ 0'.

Think of a short line with 3 words what from 30 letters, what will your software count up to 100 character ?!

 2
Author: Israel silva barbara, 2015-11-30 17:06:19