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).
#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);
}
Comments
Leave a comment