-->

program to write/store record and read from data file. THis data file contains employee name and salary.

using codeblocks
---------------------------------------------------------------------------------------------
Write a program to write/store record and read from data file. THis data file contains employee name and salary. Use fread () and fwrite().
-----------------------------------------------------------------------------------------------------
//program to store and read employee name and salary using fread() and 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("\n enter 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
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf('-----------------------');
printf("reading data\n");
k=fopen("employee.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("salary=%f and name=%s\n",var.salary,var.name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}






//or

//program to store and read employee name and salary using fscanf() and fprintf()
#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
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file
printf("now going to read data\n");
k=fopen("employee.txt","r");// opening file in read mode
while((fscanf(k,"%f %s",&var.salary,var.name))!=EOF)// reading data until it reads the last one
{
printf("name=%s and salary=%f\n",var.name,var.salary);//printing that read data
}
fclose(k);
getch();
return 0;
}
--------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------

//program to store and read employee name and salary using fread() and 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("\n enter 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
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf('-----------------------');
printf("reading data\n");
k=fopen("employee.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("salary=%f and name=%s\n",var.salary,var.name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}






//or

//program to store and read employee name and salary using fscanf() and fprintf()
#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
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file
printf("now going to read data\n");
k=fopen("employee.txt","r");// opening file in read mode
while((fscanf(k,"%f %s",&var.salary,var.name))!=EOF)// reading data until it reads the last one
{
printf("name=%s and salary=%f\n",var.name,var.salary);//printing that read data
}
fclose(k);
getch();
return 0;
}

No comments:

Post a Comment