Code Competitions

Coding competition

Monday, 13 May 2013

Program to search any element from file list

#include<stdio.h>
#include<conio.h>
void main()
{

    int a[20],i,n,ch,item;
    FILE *fp;
    clrscr();
    printf("enter the number of elements ");
    scanf("%d",&n);
    printf("\nenter the elements of the list ");
    fp=fopen("org.txt","w");
    for(i=0;i<n;i++)
    {
        scanf("%d",&ch);
        fprintf(fp,"%d\t",ch);
    }
    fclose(fp);
    fp=fopen("org.txt","r");
    //printf("the entered series is ");
    //fscanf(fp,"%d",&ch);
    for(i=0;i<n;i++)
    {
        fscanf(fp,"%d",&ch);
        printf("%5d",ch);

    }
    fclose(fp);
    fp=fopen("org.txt","r");
    printf("\nenter the searching element ");
    scanf("%d",&item);
    for(i=0;i<n;i++)
    {
        fscanf(fp,"%d",&ch);
        if(item==ch)
        {
            printf("\nitem is present at location %4d\n",i+1);
        }
    }
    getch();
}

No comments:

Post a Comment