I wrote a c program to count the number of time the word "printf" occurs in a specific file (here "document.c"). "document.c" has multiple lines of code. What I have done is I started with a while loop to iterate over every lines of the file and then I am reading the characters of each lines inside the for loop by using the function strstr.
It does not print anything with my current code. Moreove, I think there is some other minor issues because in an older version it used to print but not correctly, it printed a number much more larger than the actual number of "printf" in the document.
I am also novice in c.thank you!
int counter() {
FILE * filePointer;
filePointer = fopen("document.c", "r");
int counter = 0;
char singleLine[200];
while(!feof(filePointer)){
fgets(singleLine, 200, filePointer);
for (int i = 0; i < strlen(singleLine); i++){
if(strstr(singleLine, "printf")){
counter++;
}
}
}
fclose(filePointer);
printf("%d",counter);
return 0;
}