So, my program has a search option to search from a file. But here, I'm using strcmp function so the strings has to match exactly with the compared string.
My instructor told me to use strncmp and strlen to make the search option better. Although I don't know how to use it.
Here is the code:
Code:
int search()
{
char target[50];
int flag=0;
struct item food;
FILE *pfile;
pfile = fopen("food menu.txt","rb");
if(pfile != NULL)
{
printf("Enter Item name\n");
scanf("%s",&target);
fflush(stdin);
do
{
fread(&food,sizeof(food),1,pfile);
if(!feof(pfile));
{
if(strcmp(target,food.item_name)==0)
{
printf("\n%-20s %18s %28.2f\n",food.item_name,food.item_type,food.item_price);
flag=1;
system("pause");
return SUCCESS;
}
}
}while(!feof(pfile));
}fclose(pfile);
if(flag==0)
{
printf("NO MATCH FOUND");
}
}
Any help would be appreciated. Thanks.