0

I'm attempting to use C to search a file that contains C code. It is meant to search through the entire file, find certain keywords or characters (Such as looking for Ints, Longs, For loops, etc.) and logs them by incrementing a counter, as well as counting all the total lines of code. Then it's meant to provide the total number of each so percentages can be calculated based upon how often a keyword appears in the file.

However, i'm having trouble getting the code to recognize the keywords. How should I get this to read the total lines of the code, as well as look for keywords?

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

#define _CRT_SECURE_NO_WARNINGS

/*  Count and compute:

    number of total lines
    number and percentage of blank lines
    number and percentage of comments (start with // or /*)
    number and percentages of ints, longs, floats, doubles, char
    number and percentages of if's
    number and percentage of else's
    number and percentage of for's
    number and percentage of switch
    number and percentage of semicolons
    number and percentage of structs
    number and percentage of arrays (contains [ or ], divide count by 2)
    number of blocks (contains { or }, divide count by 2)
*/


int main(void)
{
    int lineCount = 0;  // Line counter (result) 
    int forCount = 0; // For counter
    int intCount = 0;
    char c;

    FILE *ptr_file;
    char buf[1000];

    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {


        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == '\n') // Increment count if this character is newline 
                lineCount = lineCount + 1;
        }
    }
    fclose(ptr_file);
    //End of first scan
    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {
        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == 'for') // Increment count if this character is for
                forCount = forCount + 1;
        }
    }
    fclose(ptr_file);
    //End of second scan
    ptr_file = fopen("file.txt", "r");
    if (!ptr_file)
        return 1;

    while (fgets(buf, 1000, ptr_file) != NULL) {
        for (c = getc(ptr_file); c != EOF; c = getc(ptr_file)) {
            if (c == 'int') // Increment count if this character is for
                intCount = intCount + 1;
        }
    }

    fclose(ptr_file);
    printf("\nThe file has %d lines\n", lineCount);
    printf("\nThe file has %d fors\n", forCount);
    printf("\nThe file has %d ints\n", intCount);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • `#define`ing `_CRT_SECURE_NO_WARNINGS` after including the standard headers has no effect. You'd have to define it before including the header files. – Swordfish Feb 25 '19 at 02:30
  • For the majority of things you want to count you'd have to build an AST. Consider using libclang. – Swordfish Feb 25 '19 at 02:44

2 Answers2

1

You need to use sscanf and parse it line by line.

For each item found keeping a tally should be no issue.

But as you discussed (asked for help on that other forum) the function you need is this one.

solarflare
  • 423
  • 3
  • 14
0

Getting an exact answer may require more complex parsing than you think: consider for a moment that a long may also be declared as a long int, and that either long long or long long int are also valid variable declarations. Furthermore, you can declare several variables on the same line, and you don't want to count instances where int is part of a longer word.

For a quick approximation, the Linux tools grep, and wc can be helpful:

  • wc -l filename will list the number of lines of a file
  • grep "for" filename | wc -l will list the number of lines in which a for is included

Note that these are approximations: if for occurs more than once on a line, or for is part of another word like forth, one instance will still be counted.

TimD1
  • 982
  • 15
  • 26