Answer to Question #214622 in C for a.s.vijay

Question #214622

The file named called EMPLOYEE.txt contains employee details such as employee number, name, designation and salary of employee. Write a program to create a file to store the details of employee (Use of structure and fwrite () & fread () functions).


1
Expert's answer
2021-07-07T18:26:12-0400
 #include<stdio.h>


       struct Employee
       {
    int employee_number;
	char name [40];
	char designation [50];
	float salary_of_employee;
       };


       void main()
       {
              FILE *file;
              char chr;
              struct Employee em;


              file = fopen("EMPLOYEE.txt","w");            


              if(file == NULL)
              {
                     printf("\nError.");
                     exit(0);
              }


              do
              {
                      printf("\nEmployee Number : ");
                      scanf("%d",&em.employee_number);


                      printf("Enter Employee Name : ");
                      scanf("%s",&em.name);


                      printf("Enter Employee designation : ");
                      scanf("%c",&em.designation);
                      
                      printf("Enter Employee Salary : ");
                      scanf("%f",&em.salary_of_employee);


                      fwrite(&em,sizeof(em),1,file);


                      printf("\nEnter another data (y/n) : ");
                      chr = getche();


              }while(chr=='y' || chr=='Y');


              printf("\nEnter y to continue");


              fclose(file);
       }







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS