-->
Showing posts with label program to read data stored in a datafile and print on screen using fread().. Show all posts
Showing posts with label program to read data stored in a datafile and print on screen using fread().. Show all posts

program to read data stored in a datafile and print on screen using fread().

using codeblocks
---------------------------------------------------------------------------------------------------
 program to read data stored in a datafile and print on screen using fread()(previous question).
--------------------------------------------------------------------------------------------------------------
//program to store and read 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
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;
}

--------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------

//program to store and read 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
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;
}

//note: fread() does not accept EOF()/feof() function.