Hello, I'm still a C beginner. I am trying to make a simple code for searching a string in a doc file.
I want to search for a name in the "Employee Database", and display the employee data no,name,desgnation,reg no.,Projects worked.
The template in the file is as follows:
Employee xx
Name : SOME_NAME
Designation : SOME_DES
Employee no. : xx
No. of projects worked : xx
where x is integers.
I have a problem fetching things from the buffer array. I get some long output of this form : ||[[-]||| ....etc.,
Here is my code:
void searchf()
{
char S[500],I[500],B[500][50],T[500],K[500][5];
int e=0,flag=0,i,l,v=0;
FILE *j;
printf("\n\nIn which file do you want a name to be searched?\nEnter the complete address below\n\n");
scanf("%s",I);
j=fopen(I,"r");
if(j == NULL)
{
printf("ERROR : File not found. Try again later\n");
getch();
exit(1);
}
printf("Input name of employee to be found\n");
scanf("%s",S);
strcpy(T," Name : ");
strcat(T,S);
strcat(T,"\n");
while(fgets(B[e],100,j)!=NULL)
{
if(strcmp(T,B[e])==0)
{
strcpy(K[0],B[e-1]);
strcpy(K[1],B[e]);
strcpy(K[2],B[e+1]);
strcpy(K[3],B[e+2]);
strcpy(K[4],B[e+3]);
flag=1;
goto end;
}
e++;
}
fclose(j);
end:
if(flag==0)
printf("Employee not found!");
else
printf("Employee found in database.\nEmployee details:\n %s%s%s%s%s",K[0],K[1],K[2],K[3],K[4]);
}
Please help me. Thanks in advance!