-->

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