I am using the following program to print the document GPA.txt and to figure out how many times the word "pets" is in that document.
I manually counted it and it's in there 4 times.
The document prints, but it keeps telling me that 'pets' is not in the file (0 times).
Please let me know what I am doing wrong.
Thank you
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main (){
char c;
char inputText[500];
int countSame=0;
//printf("Please enter the name of your file:");
//scanf("%s",fileName);
FILE* inputFile;
inputFile=fopen("C:\\Documents and Settings\\User\\My Documents\\GPA.txt","r");
if (inputFile==NULL){
printf("Failed to open");
exit(-1);
}
fgets(inputText, 500, inputFile);
while ((c=getc(inputFile))!=EOF){
printf("%c",c);
if( strcmp( inputFile, "pets" ) == 0 ) {
countSame++;
}
}
printf("There are %i matches for pets", countSame);
system("Pause");
}