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);
}