-->

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.

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;
}
*/

Program to store and read roll,name and percentage of some students to /from a data file using same program.

using codeblocks
--------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
printf("we are going to write /store data\n");
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\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
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("student.txt","r");//file opening
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file
getch();
return 0;
}

Program to read roll,name and percentage of some students from a data file

using codeblocks
-----------------------------------------------------------------------------------------
//reading data from a data file
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                          //       "             "
float per;
k=fopen("student.txt","r");          //opening of file in write mode
printf("records are\n");
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}


---------------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------------------------------------------
//reading data from a data file
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                          //       "             "
float per;
k=fopen("student.txt","r");          //opening of file in write mode
printf("records are\n");
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}

Program to store roll,name and percentage of some students in a data file

using codeblocks
--------------------------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\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                       
getch();
return 0;
}

//or
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                   //       "             "
float per;
k=fopen("student.txt","w");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}


---------------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\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                       
getch();
return 0;
}

//or
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                   //       "             "
float per;
k=fopen("student.txt","w");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}

Program to store and read data stored in a data file using fprintf() and fscanf() function

using codeblocks
-------------------------------------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       fprintf(p,"%d\n",i);                        // write the entered integer in that file
    }
    fclose(p);                              //closes the file
    getch();
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)    //read an integer using fscanf() from opened file util it ends.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)             //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/
--------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       fprintf(p,"%d\n",i);                        // write the entered integer in that file
        
    }
    fclose(p);                              //closes the file
    getch();
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)    //read an integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/

program to read that stored integers using getw

using codeblocks
-----------------------------------------------------------------------------
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}

----------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    getch();
    return 0;
}