0% found this document useful (0 votes)
7 views

Linear Search Over File

Linear Search Over Txt File

Uploaded by

rahuls.acbcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Linear Search Over File

Linear Search Over Txt File

Uploaded by

rahuls.acbcs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

/*a) Read the data from file 'cities.

txt' containing names of cities


and their STD codes. Accept a name of the city from user and use
linear search algorithm to check whether the name is present in the
file and output the STD code, otherwise output “city not in the
list”.

Solution:*/

#include<stdio.h>
#include<string.h>
typedef struct city
{
char name[20];
int code;
}record;
record city[100];
int read_file(record *a)
{
int i=0;
FILE *fp;
if((fp=fopen("cities.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%s%d",a[i].name,&a[i].code);
i++;
}
}
return (i-1);
}
void l_search(record *a,int n,char x[20])
{
int i;
for(i=0;i<n;i++)
{
if(strcmp(a[i].name,x)==0)
{
printf("\n%s=%d\n",a[i].name,a[i].code);
break;
}
}
if(i==n)
printf("\n%s City not in the list\n",x);
}
int main()
{
char x[20];
int n;
n=read_file(city);
printf("\nEnter city name\n");
gets(x);
l_search(city,n,x);
return 0;
}

You might also like