This program will allow the user to input string then scans the file if it contains the same string given by the user. But i always get "MATCHED" even if i enter random string. Please help. I tried and tried to place the if statement in different positions but i dont get my expected output.
Code:
#include<stdio.h>#include<stdlib.h>
#include<time.h>
#include<string.h>
int main()
{
int found;
char str[512];
char string[512];
int count=0;
char a,b;
do
{
printf("ENTER STRING: ");
scanf("%s", &string);
FILE *fp;
fp=fopen("JL-answers[easy].txt", "r+");
if (fp==NULL)
{
printf("ERORR.\n");
exit(1);
}
while(!feof(fp))
{
fscanf(fp, "%s", str);
if (strcmpi(string, str)==0)
{
found = 1;
}
}
if (found<=1)
{
printf("MATCHED\n");
}
else
{
printf("STRING NOT FOUND\n");
}
fclose(fp);
count++;
}
while(count<=15);
return 0;
}