-->

program to store/write employee name and salary in a datafile using fwrite()

using codeblocks
-------------------------------------------------------------------------------
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.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                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}








//or
/*
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fprintf(k,"%f %s\n",var.salary,var.name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}
*/

-----------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.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                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}








//or
/*
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fprintf(k,"%f %s\n",var.salary,var.name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}
*/

No comments:

Post a Comment