Monday, January 24, 2011

program to store the information about students in file and retrieve information arranging in alphabetical order


#include<stdio.h>
#include<string.h>
int main()
{
    struct student
    {
        char name[20],address[20],phone[15];
        int roll;
    }s[20];
    int i,j,n;
    FILE *fp;

    struct student temp;
    fp=fopen("std.dat","wb");
    printf("Enter the no. of student:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        fflush(stdin);
        printf("Enter the name of student%d:",i+1);
        gets( s[i].name);
        printf("Enter address:");
        gets( s[i].address);
        printf("Enter phone No:");
        gets( s[i].phone);
        printf("Enter roll no:");
        scanf("%d",&s[i].roll);
    }
    fwrite(s,sizeof(s[0]),1,fp);
    fclose(fp);
    fp=fopen("std.dat","rb");
    fread(s,sizeof(s[0]),1,fp);
    for(i=0;i<n-1;i++)
    for(j=i+1;j<n;j++)
    if(strcmp(s[i].name,s[j].name)>0)
    {
        temp=s[i];
        s[i]=s[j];
        s[j]=temp;
    }
    for(i=0;i<n;i++)
    {
        printf("\nName of student%d is %s",i+1,s[i].name);
        printf("\nAddress is %s",s[i].address);
        printf("\nPhone no is %s",s[i].phone);
        printf("\nRoll No is %d",s[i].roll);
    }
    return 0;
}

No comments:

Post a Comment