0

I am attempting to get my program to read strings from another file, parse them for certain keywords, and then add to a counting variable whenever they appear in the other file. However, I can't seem to get anything but the number of lines to count. What could I be doing wrong here?

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

// check that fp is not null omitted but needed
const char getLine[1] = "";
const char getFor[4] = "for";
char line[500];
int lineCount = 0;
int forCount = 0;
int x = 0;
int main() {
    FILE* fp = fopen("file.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
        return(-1);
    }
        while (fgets(line, 499, fp) != NULL) {
            strstr(line, getLine);
            lineCount++; //Essentially counting the number of lines in file.
        }

    printf("Line count is %d.\n", lineCount);
    memset(line, 0, sizeof(line)); //Resetting the memory of line.
    while (fgets(line, 499, fp) != NULL) {
        char *findFor;
        findFor = strstr(line, getFor);
        if (findFor != NULL) { //Attempting to count each time an instant of 'for' appears.
            forCount++;
        }

    }

    printf("For count is %d.\n", forCount);
    fclose(fp);
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Is the keyword `for` in the line `"foobarforqux"`? in "`foobar for(qux`"? in `"foobar for qux`"? In the first case, `strstr()` is your friend. For the 2nd you'd have to also check if `"for"` is the very first thing of the line or preceded by whitespace and followed by "`(`". For the third the same applies, but the `"for"` must also be followed by whitespace and `sscanf()` would be your friend. But as I said in response to your previous question about basically the same topic, parsing C is not easy and would require to build an AST go get it right. You might want to use `libclang`. – Swordfish Feb 25 '19 at 06:06

1 Answers1

0

The code is reading through the whole file to count the lines, but then trying to read through it again (without a rewind() / fseek()). So on the second loop the file is at end-of-file.

It's not necessary to count the lines, and the "for"s in two separate loops, just do it in one.

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

// check that fp is not null omitted but needed
const char getFor[4] = "for";
char line[500];
int lineCount = 0;
int forCount = 0;

int main(  )
{
    FILE *fp = fopen( "file.txt", "r" );
    if ( fp == NULL )
    {
        perror( "Error opening file" );
        return ( -1 );
    }

    while ( fgets( line, sizeof( line ), fp ) != NULL )
    {
        char *findFor;
        findFor = strstr( line, getFor );
        if ( findFor != NULL )
        {                       
            // Attempting to count each time an instance of 'for' appears.
            forCount++;
        }
        lineCount++;
    }

    printf( "Line count is %d.\n", lineCount );
    printf( "For count is %d.\n", forCount );
    fclose( fp );

    return 0;
}

Also you're not counting the number of "for"s in the file, you're counting the number of lines with "for" in them. If a line has multiples, it's just counted as one.

Kingsley
  • 14,398
  • 5
  • 31
  • 53