Let understand how the File Handling is used in C programming.Let see the following C program that will show you how to save our output in file.
Example
#include<stdio.h> struct employee { int eid; char ename[20]; float salary; }; int main() { int i,n; float da,hra,Tsalary; struct employee e[100]; FILE *fp; fp = fopen("D:\\text\\op1.txt","w"); printf("How many records you want to insert:"); scanf("%d",&n); printf("***Enter details of %d employees***",n); for(i=0;i<n;i++) { printf("\n Enter employee id:"); scanf("%d",&e[i].eid); printf(" Enter name of employee:"); scanf("%s",e[i].ename); printf(" Enter salary of employee:"); scanf("%f",&e[i].salary); } printf("\n ****The employee details have been saved in Op1.txt You can check now:****"); for(i=0;i<n;i++) { fprintf(fp,"\nEmployee id:%d",e[i].eid); fprintf(fp,"\nName of Employee:%s",e[i].ename); fprintf(fp,"\nSalary of Employee:%.2f₹",e[i].salary); da=e[i].salary+e[i].salary*0.02; hra=e[i].salary+e[i].salary*0.03; Tsalary=da+hra; fprintf(fp,"\nFinal salary of %s is:%.2f₹\n",e[i].ename,Tsalary); } return 0; }
Input
As you can see clearly we have entered all the values in structure fields.
and whatever the input we have entered is stored in file name "op1.txt"
and whatever the input we have entered is stored in file name "op1.txt"
1 Comments
I was searching this program and got here now.Good Work.
ReplyDelete