Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.c
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- struct employee {
- char name[128];
- float salary;
- };
- typedef struct employee Employee;
- float total_salary(Employee a[], int len);
- int main() {
- int num,i=0,j=0;
- Employee *all;
- FILE *fp;
- printf("Enter num: ");
- scanf("%d",&num);
- all=(Employee *)malloc(num*sizeof(Employee));
- printf("\nInput---------\n");
- fp=fopen("employee.bin","w+");
- while(i<num){ fflush(stdin);
- printf("[%d]name : ",i+1);
- gets((all+i)->name);
- printf("[%d]salary : ",i+1);
- scanf("%f",&(all+i)->salary);
- fwrite(&all,sizeof(Employee),1,fp);
- i++;
- }
- fclose(fp);
- fp=fopen("employee.bin","r+");
- printf("\nRead---------\n");
- fread(&all,sizeof(Employee),1,fp);
- for(i=0; i<num; i++){
- printf("%s:%.2f\n",(all+i)->name,(all+i)->salary);
- }
- fclose(fp);
- printf("\nTotal Salary = %.1f\n", total_salary(all, num));
- free(all);
- fclose(fp);
- return 0;
- }
- float total_salary(Employee a[], int len){
- int i;
- float sum=0;
- for(i=0; i<len; i++)
- sum+=(a+i)->salary;
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement