Comment count function and other program by command line in C [closed]

-2

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

Counter ProgramExample.c -c

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

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

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

// print hello

asked by anonymous 14.04.2014 / 22:43

1 answer

2

For a program with reduced needs like yours, you do not need to complicate.

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

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

So your program 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 // ... you should check if one of the parameters and -c is assumed to be the other and the name of a file.

Try to open the file, if it gives an error it informs the user of invalid file, if it does not give error, it counts the comments.

    
14.04.2014 / 22:59