Function count comments and another program per C command line

Asked

Viewed 620 times

-2

I’m having trouble knowing how to develop a function that via command line will count and display the amount of commented lines of another program in . c, for example:

Counter Programexample. c -c

The -c argument will call run the function to count the line, any other argument will report invalid.

However it is necessary to do in different files what I have done is this. This function has other arguments, but the important thing is only the line count (this function is so pq was the "recommended" in the work).

Comments that can be counted have to be of a line // or block /* */ those that are after some other action need not be counted, for example:

// prints hello <-- This account

printf ("hello world"); // this is a hello world <<-- this does not need to count

/* hello world */ <-- This account

Filing cabinet: Libcodecount. c

/* ****************************************************
 * Função para contar Quantidade de linhas comentadas *
 * de um arquivo .c lido                              *
 ******************************************************/
#include <stdio.h>
#include "LibCodeCount.h"

void ccRun(char* file, int* nLines, int* nLinesComent, int* nLinesEmpty, int noComment, int silent)
{

}

Filing cabinet Libcodecount. h

/* ********************************************
 * Cabeçalho Header da função LibCodeCount.c  *
 ******************************************** */

void ccRun(char* file, int* nLines, int* nLinesComent, int* nLinesEmpty, int noComment, int silent);

Filing cabinet Maincodecount. c

/* *********************************************************
 * Função Main que irá executar o programa para a contagem *
 * de linhas comentadas de outro arquivo em C              *
 ***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "LibCodeCount.h"

int main(int argc, char *argv[])
{

}

After the right function, then I will generate a . exe that when running in Dos along with argument and file will show the amount of commented lines.

I already sought to read about Argc and Argv found it a little confusing, but I would like a more advanced support even.

Thanks for your help.

  • 2

    It seems to me that you need to divide the problem into smaller parts, and if that is the case go asking about the specific problems you are encountering. For example, start by learning to (or asking about) read the arguments passed to the executable; then, how to read a disk file and process by line, and so on. Your current question asks someone to solve the whole task for you.

  • 1

    I posted the question so believing that it could help the person to answer my problem, it may seem that I want the whole task, but I really wanted to understand the steps to just create the function of reading commented lines.

1 answer

2

For a program with reduced needs like yours, you don’t need to complicate.

The arguments of function main() (the argc and the argv) indicate respectively how many and what were these arguments.

If a program runs without arguments, the value of argc sera 1.
If a program runs with 1 argument, the value of argc sera 2.
If a program runs with 2 arguments, the value of argc sera 3.
etc, etc. ...

So your show can start like this

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    if (argc != 3) {
        fprintf(stderr, "Formato de chamada invalido.\n");
        exit(EXIT_FAILURE);
    }
    // ...
    return 0;
}

Instead of the // ... you should check one of the parameters and -c and assume that the other is a file name.

Try to open the file, if error informs the invalid file user, if no error, counts the comments.

Browser other questions tagged

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