Sunday, 6 January 2013

PROGRAM TO PRINT EMPLOYEE DETAILS IN C


#include<conio.h>
#include<stdio.h>
#include<string.h>
struct employee
{
char name[20];
char address[30];
int telno;
}job[5],job1[5];
void main()
{
FILE *ptr;
int i,j,n,f;
struct employee tobj;
ptr=fopen("a.txt","w");
clrscr();
printf("enter no of ercords to insert:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter name:");
fprintf(ptr,"\n%s",job[i].name);
scanf("%s",&job[i].name);
printf("enter address:");
fprintf(ptr,"\n%s",job[i].address);
scanf("%s",&job[i].address);
printf("enter telephone no:");
fprintf(ptr,"\n%d",job[i].telno);
scanf("%d",&job[i].telno);
}
fclose(ptr);
ptr=fopen("a.txt","r");
for(i=0;i<n;i++)
{
fscanf(ptr,"\n%s",&job1[i].name);
fscanf(ptr,"\n%s",&job1[i].address);
fscanf(ptr,"\n%d",&job1[i].telno);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
f=strcmpi(job1[j].name,job1[j+1].name);
if(f>0)
{
tobj=job1[j];
job1[j]=job1[j+1];
job1[j+1]=tobj;
}
else
{
f=strcmpi(job1[j].address,job1[j+1].address);
if(f>0)
{
tobj=job1[j];
job1[j]=job1[j+1];
job1[j+1]=tobj;
}
}
}
}
fclose(ptr);
ptr=fopen("d.txt","w");
for(i=0;i<n;i++)
{
printf("\n\nname %s",job1[i].name);
fprintf(ptr,"\nname:%s",job1[i].name);
printf("\naddress %s",job1[i].address);
fprintf(ptr,"\naddress %s",job1[i].address);
printf("\ntelno %d",job1[i].telno);
fprintf(ptr,"\ntelno %d",job1[i].telno);
}
fclose(ptr);
getch();
}

No comments: