I am making a phone book, and this code is suppose to b used to search for the name in the file, but it keeps saying contact not found, even if the name exist in the file.. please help?
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 300
struct contact
{
char lastName [30] ;
char firstName [30];
char adress [100];
char city [25];
char phone [20];
};
int main()
{
struct contact new1[SIZE];
FILE *open;
int i=0,k;
int x,j=0;
char ln[30];
char c;
char *ptr1;
char *ptr2;
int flag=0, anotherFlag = 0;
open = fopen("C:\\Users\\Roumia\\Desktop\\contacts.txt","r");
if (open == NULL)
{
printf("file doesn't exist !\n");
}
else{
printf("file exist !\n");
while(!(feof(open)))
{
fscanf(open,"%[^,],%[^,],%[^,],%[^,],%s",new1[i].lastName,new1[i].firstName,new1[i].adress,new1[i].city,new1[i].phone);
fscanf(open,"\n");
i++;
}
}
printf("Please enter the last name of the person you need to find: ");
while((c=getchar())!='\n' && j<30)
{
ln[j++]=c;
}
ln[j] = '\0';
ptr1=ln;
for(k=0;k<i;k++){
ptr2=new1[k].lastName;
ptr1 = ln;
for(x=0;x<j;x++)
{
if(*ptr1 != *ptr2)
{
flag = 1;
}
ptr1++;
ptr2++;
}
if(flag==0)
{
anotherFlag = 1 ;
printf("Contact was found !!\n");
printf("%d\n",k);g
printf("%s,%s,%s,%s,%s",new1[k].lastName,new1[k].firstName,new1[k].adress,new1[k].city,new1[k].phone);
}
}
if(anotherFlag==0)
{
printf("Contact wasn't found !!\n");
}
return 0;
}