-->
Showing posts with label program to store detail of employees. Show all posts
Showing posts with label program to store detail of employees. Show all posts

program to store detail of employees

using codeblocks
-------------------------------------------------------------------------------------------------

/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee                           //struct tag
{
    int employee_id;
    float employee_salary;
    char employee_name[50];
}var;                                      //variable
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                              //identifier declaration
k=fopen("employees.txt","w");             //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter  employee name\n");       // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");             //opening file in read mode
k=fopen("employees.txt","r");               //file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                        //As it becomes less than 1, it stops
{
printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}






//or
/*program to store book_id,book_name and book_price of some books to a data file
 until you say 'n'. Then same file displays all records.

#include <stdio.h>
#include<conio.h>
struct employee                           //struct tag
{
    int employee_id;
    float employee_salary;
    char employee_name[50];
}var;                                      //variableint main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                               //identifier declaration
k=fopen("employees.txt","w");                  //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter  employee name\n");       // getting inputs
scanf("%s",var.employee_name);
fprintf(k,"%d %f %s\n",var.employee_id,var.employee_price,var.employee_name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");
k=fopen("employees.txt","r");//file opening

while((fscanf(k,"%d %f %s",&var.employee_id,&var.employee_price,var.employee_name))!=EOF)
                                        // fscanf  reads the records till it is the last one.
                                        //As it ends, it stops
{
printf("employee id=%d,employee price=%f,employee name=%s\n",var.employee_id,var.employee_price,var.employee_name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}
*/

same program is also for turbo c++.