/*………………………………………………………………………………………...............
File Name: rec26.c
Program : File program to demonstrate the use of fread() and fwrite(). Define a structure to store the details of employees(Emp_ID,Emp_Name,Emp_Sal etc..)and store into some file using fwrite() and read the same using fread().
Author : sujithbaby440@gmail.com
Date : 26-11-2014
……………............................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
typedef struct employee{
int emp_id;
char emp_name[20];
float emp_sal;
}EMPLOYEE;
EMPLOYEE E[20],EMP[20];
main()
{
FILE *fp;
int i,j,n;
system("clear");
printf("Program to store the details of employee\n");
fp=fopen("employee.txt","w");
if(fp!=NULL){
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the employee id:\n");
scanf("%d",&E[i].emp_id);
printf("Enter employee name:\n");
scanf("%s",E[i].emp_name);
printf("Enter the salary:\n");
scanf("%f",&E[i].emp_sal);
fwrite(&E[i],sizeof(EMPLOYEE),1,fp);
}
fclose(fp);
fp=fopen("employee.txt","r");
rewind(fp);
printf("Emp_ID\tEmp_Name\tSalary\n");
for(i=1;i<=n;i++){
fread(&EMP[i],sizeof(EMPLOYEE),1,fp);
printf("%d\t%s\t\t%.2f\n",EMP[i].emp_id,EMP[i].emp_name,EMP[i].emp_sal);
}
fclose(fp);
}
else
printf("Error in opening file");
}
OUTPUT 1:-
Program to store the details of employee
Enter the value of N:3
Enter the employee id:
212
Enter employee name:
Sujith
Enter the salary:
25000
Enter the employee id:
345
Enter employee name:
Renjith
Enter the salary:
28000
Enter the employee id:
651
Enter employee name:
Alan
Enter the salary:
30000
Emp_ID Emp_Name Salary
212 Sujith 25000.00
345 Renjith 28000.00
651 Alan 30000.00
OUTPUT 2
Program to store the details of employee
Enter the value of N:2
Enter the employee id:
55
Enter employee name:
Koran
Enter the salary:
35000
Enter the employee id:
65
Enter employee name:
Thinnappan
Enter the salary:
40000
Emp_ID Emp_Name Salary
55 Koran 35000.00
65 Thinnapan 40000.00
File Name: rec26.c
Program : File program to demonstrate the use of fread() and fwrite(). Define a structure to store the details of employees(Emp_ID,Emp_Name,Emp_Sal etc..)and store into some file using fwrite() and read the same using fread().
Author : sujithbaby440@gmail.com
Date : 26-11-2014
……………............................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
typedef struct employee{
int emp_id;
char emp_name[20];
float emp_sal;
}EMPLOYEE;
EMPLOYEE E[20],EMP[20];
main()
{
FILE *fp;
int i,j,n;
system("clear");
printf("Program to store the details of employee\n");
fp=fopen("employee.txt","w");
if(fp!=NULL){
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("Enter the employee id:\n");
scanf("%d",&E[i].emp_id);
printf("Enter employee name:\n");
scanf("%s",E[i].emp_name);
printf("Enter the salary:\n");
scanf("%f",&E[i].emp_sal);
fwrite(&E[i],sizeof(EMPLOYEE),1,fp);
}
fclose(fp);
fp=fopen("employee.txt","r");
rewind(fp);
printf("Emp_ID\tEmp_Name\tSalary\n");
for(i=1;i<=n;i++){
fread(&EMP[i],sizeof(EMPLOYEE),1,fp);
printf("%d\t%s\t\t%.2f\n",EMP[i].emp_id,EMP[i].emp_name,EMP[i].emp_sal);
}
fclose(fp);
}
else
printf("Error in opening file");
}
OUTPUT 1:-
Program to store the details of employee
Enter the value of N:3
Enter the employee id:
212
Enter employee name:
Sujith
Enter the salary:
25000
Enter the employee id:
345
Enter employee name:
Renjith
Enter the salary:
28000
Enter the employee id:
651
Enter employee name:
Alan
Enter the salary:
30000
Emp_ID Emp_Name Salary
212 Sujith 25000.00
345 Renjith 28000.00
651 Alan 30000.00
OUTPUT 2
Program to store the details of employee
Enter the value of N:2
Enter the employee id:
55
Enter employee name:
Koran
Enter the salary:
35000
Enter the employee id:
65
Enter employee name:
Thinnappan
Enter the salary:
40000
Emp_ID Emp_Name Salary
55 Koran 35000.00
65 Thinnapan 40000.00
No comments:
Post a Comment