Commentary count // and /* */ in C

Asked

Viewed 302 times

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?

  • I’m running and he’s playing numbers without logic as a result

1 answer

2


Hello.

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 strlen’s call

int line_size = strlen(buff_linha);

Most likely you are reading trash when reading the whole array and not just reading the string up to the ' 0 character'.

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

  • Thanks Israel! I switched the fgets for fscanf and it worked for /* but it didn’t work for // but thanks brother (y)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.