-->

program to understand rename() function

using codeblocks
-------------------------------------------------------------------------------------------------
//understanding rename() function
#include <stdio.h>
#include<conio.h>

int main()
{
  char first_name[40],second_name[40];

  printf("enter file name with extension .txt\n");
  gets(first_name);
  printf("enter name to be changed\n");
  gets(second_name);
    rename("first_name","second_name");
    printf("the entered file renamed successfully");
    getch();
    return 0;
}

//note: We do not need to open file here. We can do without opening too(from line 19).
/*i.e
//understanding rename() function
#include <stdio.h>
#include<conio.h>
int main()
{
  char first_name[40],second_name[50];
  printf("enter file name with extension .txt\n");
  gets(first_name);
    printf("enter name to be changed\n");
    gets(second_name);
    rename("first_name","second_name");
    printf("the entered file renamed successfully");
    getch();
    return 0;
}
*/

-------------------------------------
same program, we can use for turboc++ as well

Program to understand remove() function.

using codeblocks
--------------------------------------------------------------------------------------------
//understanding remove() function
#include <stdio.h>
#include<conio.h>

int main()
{
  char file_name[40];
  FILE *k;
  printf("enter file name with extension .txt\n");
  gets(file_name);

k=fopen("file_name","w");
if(k==NULL)
{
    printf("sorry,  not found\n ");
}
else
{
    remove("file_name");
    printf("the entered file removed successfully");
}
    getch();
    return 0;
}

//note: We do not need to open file here. We can do without opening too(from line 19).
/*i.e
//understanding remove() function
#include <stdio.h>
#include<conio.h>

int main()
{
  char file_name[40];
  printf("enter file name with extension .txt\n");
  gets(file_name);

    remove("file_name");
    printf("the entered file removed successfully");

    getch();
    return 0;
}
*/

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++.


program to store book_id,book_name and book_price of some books.

using codeblocks
--------------------------------------------------------------------------------------------------------
/*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 <stdlib.h>
#include<conio.h>
struct book                           //struct tag
{
    int book_id;
    float book_price;
    char book_name[50];
}var;                                      //variable
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                             //identifier declaration
k=fopen("book.txt","w");                  //file opening in write mode
do
{
printf("\n enter book id\n");
scanf("%d",&var.book_id);
printf("enter book price\n");
scanf("%f",&var.book_price);
printf("enter  book name\n");           // getting inputs
scanf("%s",var.book_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("book.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("book id=%d,book price=%f,book name=%s\n",var.book_id,var.book_price,var.book_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 <stdlib.h>
#include<conio.h>
struct book
{
    int book_id;
    float book_price;
    char book_name[50];
}var;
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                               //identifier declaration
k=fopen("book.txt","w");                  //file opening in write mode
do
{
printf("\n enter book id\n");
scanf("%d",&var.book_id);
printf("enter book price\n");
scanf("%f",&var.book_price);
printf("enter  book name\n");           // getting inputs
scanf("%s",var.book_name);
fprintf(k,"%d %f %s\n",var.book_id,var.book_price,var.book_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("book.txt","r");//file opening

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

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

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