Anda di halaman 1dari 6

/* NAME : SANDEEP

CLASS :CS S3
ROLL NO :40
Do the above program using files in C */

#include<conio.h>
#include<stdio.h>
#include<string.h>
struct allowance
{ int hra,ta,da;
};
struct employee
{ int id;
char ename[10];
int bsal,netsal;
struct allowance a;
}e[10],t;
void main()
{ int n,i,j;
FILE *p;
clrscr();
printf("enter the no of employees");
scanf("%d",&n);
printf("enter the detais");
for(i=0;i<n;i++)
{ printf("\n enter id & name");
scanf("\n%d\n%s",&e[i].id,e[i].ename);
printf("\nenter basic salary");
scanf("\n%d",&e[i].bsal);
printf("\nenter hra ta & da ");
scanf("\n%d\n%d\n%d",&e[i].a.hra,&e[i].a.ta,&e[i].a.da);
e[i].netsal=e[i].bsal+e[i].a.hra+e[i].a.ta+e[i].a.da;
}
for(i=0;i<n-1;i++)
{ for(j=0;j<n-i-1;j++)
{ if(strcmp(e[j].ename,e[j+1].ename)>0)
{ t=e[j];
e[j]=e[j+1];
e[j+1]=t;
}
}
}
p=fopen("employee.txt","w");
for(i=0;i<n;i++)
{ fprintf(p,"%d %s %d
%d\n",e[i].id,e[i].ename,e[i].bsal,e[i].netsal);
}
fclose(p);
p=fopen("employee.txt","r");
printf("\ndetails after sorting are\n");
for(i=0;i<n;i++)

{ fscanf(p,"%d%s%d%d",&e[i].id,e[i].ename,&e[i].bsal,e[i].netsal);
printf("\nemlplyee id is : %d",e[i].id);
printf("\nemployee name is : %s",e[i].ename);
printf("\nbasic salary is : %d",e[i].bsal);
printf("\nnet salary is : %d",e[i].netsal);
}
fclose(p);
getch();
}
output
enter the no of employees 2
enter the detais
enter id & name 45 jithin
enter basic salary 5200
enter hra ta & da 12 45 12
enter id & name 75 ajay
enter basic salary 8452
enter hra ta & da 42 12 452

Details after sorting are

emlplyee id is : 75
employee name is : ajay
basic salary is : 8452
net salary is : 8958

emlplyee id is : 45
employee name is : jithin
basic salary is : 5200
net salary is : 5269

/* NAME : SANDEEP
CLASS :CS S3
ROLL NO :40
Write a program to sort number using pointers */

#include<stdio.h>
#include<conio.h>
void main()
{ int *p,n,i,j,t;
clrscr();
printf("enter the no of numbers\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
printf("enter the numbers\n");
for(i=0;i<n;i++)
{ scanf("%d",p+i);
}
for(i=0;i<n-1;i++)
{ for(j=0;j<n-1-i;j++)
{ if(*(p+j)>*(p+j+1))
{ t=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=t;
}
}
}
printf("\nsortted nos are\n");
for(i=0;i<n;i++)
{ printf("\n%d",*(p+i));
}
getch();
}

Output
enter the no of numbers
5
enter the numbers
4
1
7
5
3

sortted nos are

1
3
4
5
7

/* NAME : SANDEEP
CLASS :CS S3
ROLL NO :40
Write a program to merge files */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char ch;
clrscr();
fp1=fopen("1st.txt","w");
printf("\nEnter first data\n");
do
{ ch=getchar();
putc(ch,fp1);
}while(ch!='\n');
fclose(fp1);
fp2=fopen("2nd.txt","w");
printf("\nEnter second data\n");
do
{ ch=getchar();
putc(ch,fp2);
}while(ch!='\n');
fclose(fp2);
fp1=fopen("1st.txt","r");
fp2=fopen("2nd.txt","r");
fp3=fopen("3rd.txt","w");
while((ch=getc(fp1))!=EOF)
{
putc(ch,fp3);
}
while((ch=getc(fp2))!=EOF)
{
putc(ch,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("3rd.txt","r");
printf("\nMerged contents\n");
while((ch=getc(fp3))!=EOF)
{
putchar(ch);
}
fclose(fp3);
getch();
}

Output

Enter first data


hai

Enter second data


hallo everyone

Merged contents

hai
hallo everyone

Anda mungkin juga menyukai