C program to Writes records to a file using structure

C program to Writes records to a file using structure

#include "stdio.h" 
main( ) 
{ 
 FILE *fp ; 
 char another = 'Y' ; 
 struct emp 
 { 
 char name[40] ; 
 int age ; 
 float bs ; 
 } ; 
 struct emp e ; 
 fp = fopen ( "EMPLOYEE.DAT", "w" ) ; 
 if ( fp == NULL ) 
 { 
 puts ( "Cannot open file" ) ; 
 exit( ) ; 
 } 
 while ( another == 'Y' ) 
 { 
 printf ( "\nEnter name, age and basic salary: " ) ; 
 scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ; 
 fprintf ( fp, "%s %d %f\n", e.name, e.age, e.bs ) ; 
 printf ( "Add another record (Y/N) " ) ; 
 fflush ( stdin ) ; 
 another = getche( ) ; 
 } 
 fclose ( fp ) ; 
} 

And here is the output of the program…
Enter name, age and basic salary: Sunil 34 1250.50
Add another record (Y/N) Y
Enter name, age and basic salary: Sameer 21 1300.50
Add another record (Y/N) Y
Enter name, age and basic salary: Rahul 34 1400.55
Add another record (Y/N) N

Leave a Comment