-->

program to get co-ordinate of clicked button of mouse

using turboc++
------------------------------------------------------------------------------
//program to display co-ordinate of x and y on click
#include <dos.h>
#include<stdio.h>
#include<conio.h>
union REGS in_put, out_put;// input and output variables stored in union
  //input for signal detection;output for screen display
void mouse_detected ()
{
in_put.x.ax = 0;// ax is register used for mouse
int86 (0X33,&in_put,&out_put);   //invokes interrupt using 0X33; for mouse, it is 0X33
if (out_put.x.ax == 0)    //condition testing
printf("\nMouse Failed To Initialize");
else
printf ("\nMouse was Succesfully Initialized");
       //initialization on screen
}

void show_mouse()
{
 in_put.x.ax = 1;   //1 is to display mouse
 int86(0X33,&in_put,&out_put); //interrupt invoked
}

void mouse_cord()
{
while(!kbhit())    //until a keystroke is pressed
{
int x,y;
in_put.x.ax=3;    //3 is for co-ordinate value
 int86 (0X33,&in_put,&out_put);
 x = out_put.x.cx;  //gets x-value
 y = out_put.x.dx;  //gets y value

 if (out_put.x.bx == 1)
   printf ("\nLeft || X - %d  Y - %d", x, y);//x and y value on left click

 else if (out_put.x.bx == 2) //x and y value on right click
   printf ("\nRight:x-%d,y-%d",x,y);
 else if (out_put.x.bx == 3)//x and y value on wheel click
   printf ("\nMiddle");
   delay (200); // Otherwise due to quick computer response 100s of words will get print
}
}

int main ()
{        clrscr();
mouse_detected ();//calling of function
show_mouse();//calling of function
mouse_cord();
getch ();
return 0;
}


program to detect click of button of mouse

using turboc++
-----------------------------------------------------------------------------------------------------
//program to detect button on click
#include <dos.h>
#include<stdio.h>
#include<conio.h>
union REGS in_put, out_put;// input and output variables stored in union
  //input for signal detection;output for screen display
void mouse_detected ()
{
in_put.x.ax = 0;// ax is register used for mouse
int86 (0X33,&in_put,&out_put);   //invokes interrupt using 0X33; for mouse, it is 0X33
if (out_put.x.ax == 0)    //condition testing
printf("\nMouse Failed To Initialize");
else
printf ("\nMouse was Succesfully Initialized");
       //initialization on screen
}
void show_mouse()
{
 in_put.x.ax = 1;   //1 is to display mouse
 int86(0X33,&in_put,&out_put); //interrupt invoked
}
void mouse_bttn()
{
while(!kbhit())    //until a keystroke is pressed
{
int x,y;
in_put.x.ax=3;    //3 is for co-ordinate value
 int86 (0X33,&in_put,&out_put);

 if (out_put.x.bx == 1)     //1 for left click
   printf ("\nLeft clicked");

 else if (out_put.x.bx == 2) //2 for right click
   printf ("\nRight clicked");
 else if (out_put.x.bx == 3)//3 for wheel click
   printf ("\nMiddle clicked");
   delay (200); // Otherwise due to quick computer response 100s of words will get print
}
}
int main ()
{
mouse_detected ();//calling of function
show_mouse();//calling of function
mouse_bttn();
getch ();
return 0;
}


Program to show pointer of mouse(text and graphics mode)

using turboc++
-----------------------------------------------------------------
//program to activate/detection mouse
#include <dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
union REGS in_put, out_put;// input and output variables stored in union
  //input for signal detection;output for screen display
void showmouse()

{
 //in_put.x.ax = 1;// taking value 1 for display of mouse on screen  in text mode
 //int86 (0X33,&in_put,&out_put);//invoking interrupt
 //if we want to display mouse in graphics mode then
 //we put following code
 int gdriver=DETECT,gmode;  //graphics detection
 initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); //graphics initialization
 in_put.x.ax = 1;// ax is register used for mouse .1 is used to display mouse
 int86 (0X33,&in_put,&out_put);   //invokes interrupt using 0X33; for mouse, it is 0X33
 getch();
 closegraph();   //graphics closing.
}
int main ()
{

showmouse();//calling of function
getch ();
return 0;
}

//note:If you want to use mouse in text mode, uncomment the commented lines
//in showmouse() function.
//we can also put mouse initialization part here.

program to activate /initialize mouse.

using turboc++
-------------------------------------------------
//program to activate/detection mouse
#include <dos.h>
#include<stdio.h>
#include<conio.h>
union REGS in_put, out_put;// input and output variables stored in union
  //input for signal detection;output for screen display
void mouse_detected ()
{
in_put.x.ax = 0;// ax is register used for mouse; 'x' is member of reg.
int86 (0X33,&in_put,&out_put);   //invokes interrupt using 0X33; for mouse, it is 0X33
if (out_put.x.ax == 0)    //condition testing
printf("\nMouse Failed To Initialize");
else
printf ("\nMouse was Succesfully Initialized");
       //initialization on screen
}
int main ()
{
mouse_detected ();//calling of function
getch ();
return 0;
}


program to move a circle.

using turboc++
--------------------------------------------------------------------------
//moving a circle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
clrscr();
int gdriver, gmode;    //variables declaration
gdriver=DETECT;        // detection of graphics driver
int move_location=20; //first location for circle to start
initgraph(&gdriver, &gmode, "c:\\tc\\BGI");
while(!kbhit()) //Checks for currently available keystrokes
{
circle(move_location,210,70);//draws circle
setcolor(BLUE);   //sets the color blue of circle
delay(100);       //delays of 1/10th of second
cleardevice();    //clears the previous screen/graphics
move_location++;
if(move_location>=600)
{
move_location=0;   //repeating the motion.
cleardevice();  //clears the graphics screen
}
}
getch();          //gets one character from user and closes the graphics.
closegraph();     //closes graphics.h
}

Program to draw triangle.

using turboc++
------------------------------------------------------------------------
//drawing triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gdriver, gmode;    //variables declaration
gdriver=DETECT;        // detection of graphics driver

initgraph(&gdriver, &gmode, "c:\\tc\\BGI");
line(300,100,300,200);//draws frist line
line(300,200,350,200);//draws second line
line(300,100,350,200);//draws third line

getch();          //gets one character from user and closes the graphics.
closegraph();     //closes graphics.h
}

Program to draw a a line.

using turboc++
--------------------------------------------------------------------------------------------
//drawing triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gdriver, gmode;    //variables declaration
gdriver=DETECT;        // detection of graphics driver

initgraph(&gdriver, &gmode, "c:\\tc\\BGI");
line(500,100,300,200);//draws line of two co-ordinates .

getch();          //gets one character from user and closes the graphics.
closegraph();     //closes graphics.h
}

program to display a text with background colour.

using turbo c++
--------------------------------------------------------------------------------------
//program to set background color.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gdriver = DETECT, gmode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setbkcolor(BLUE);       //sets background color blue
printf("hello\n");      //prints hello
printf("how r u?");
getch();
closegraph();
}

program to understand outtext() and outtextxy().

using turboc++
--------------------------------------------------------------------------------------------------------
//understanding function of outtext() and outtextxy() function.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver = DETECT, gmode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
clrscr();
outtext("hello");
// outtext displays a string in the viewport (graphics mode)
outtextxy(20,20,"different location\n");
// outtextxy displays a string at the specified location (graphics mode)
getch();
closegraph();
}

PROGRAM TO UNDERSTAND WINDOW() FUNCTION.

USING TURBOC++
-----------------------------------------------------------------------------------------
//understanding function of window() function.
#include<stdio.h>
#include<conio.h>// contains textcolor() and text background() and window().
void main()
{
clrscr();
window(10,10,40,11);     //generates window of given size
textcolor(BLUE); //sets textcolor blue
textbackground(BLACK); //Sets background black
cprintf("first line...........\n");
cprintf("second");
//sends formatted output to the text window on the screen
  //Does not work with printf.
getch();
}

/*second part prints line from that location where
first part is finished
*/

Program understand textcolor() and textbackground() function

using turboc++
------------------------------------------------------------------------------
//understanding textcolor in graphics
#include<stdio.h>
#include<conio.h>// contains textcolor() and text background().
void main()
{
clrscr();
textbackground(RED);  //Sets background red
textcolor(GREEN);    //sets textcolor green
cprintf("first line...........\n");
textcolor(GREEN);
textbackground(BLUE);
cprintf("This is a test\n");//sends formatted output to the text window on the screen
  //Does not work with printf.
getch();
}

/*second part prints line from that location where
first part is finished
*/
..............................................................................

program to understand fclose() with stdout

using codeblocks
---------------------------------------------------------------------------------------------------------
//understanding fclose() in the sense of stdout.
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello world!\n");       //prints hello world
    fclose(stdout);                 // closes the data to be written to device
    printf("goodbye");               //does not print good bye.
    return 0;
}

------------------------------------------------------------------------------------
using turboc++
--------------------------------------------------------------------------------
//understanding fclose() in the sense of stdout.
#include <stdio.h>
#include <conio.h>

int main()
{
    printf("Hello world!\n");       //prints hello world
    fclose(stdout);                 // closes the data to be written to device
    printf("goodbye");               //does not print good bye.
    getch();
    return 0;
}

other programs

program to generate random numbers

using codeblocks
---------------------------------------------------------------------------------------------
//progrm to generate random numbers
#include <stdio.h>
#include <stdlib.h>  //contains rand() function
int main()
{
    int k,number;
    printf("how many u want=\n");
    scanf("%d",&number);
    printf("some randomly generated numbers are given below.\n");
    for(k=0;k<=number;k++)
    {
        printf("%7d\t",rand());//prints randomly generated numbers
    }
    return 0;
}
----------------------------------------------------------------------------------------------
using turboc++
--------------------------------------------------------------------------------------------------------
//progrm to generate random numbers
#include <stdio.h>
#include <stdlib.h>  //contains rand() function
#include<conio.h>
int main()
{
    int k,number;
    printf("how many u want=\n");
    scanf("%d",&number);
    printf("some randomly generated numbers are given below.\n");
    for(k=0;k<=number;k++)
    {
        printf("%7d\t",rand());//prints randomly generated numbers
    }
    getch();
    return 0;
}

enum data type with value

using codeblocks
-------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find;                         //variable for object "computer"

int main()
{

   for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.

   {
       printf("location of =%d\n",find);// display that. the value increases itself.
   }
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find;                         //variable for object "computer"

    for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.

   {
       printf("location =%d\n",find);// display that. the value increases itself.
   }
    return 0;
}
*/
----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------------------------

//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find;                         //variable for object "computer"

int main()
{

   for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.

   {
       printf("location of =%d\n",find);// display that. the value increases itself.
   }
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find;                         //variable for object "computer"

    for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.

   {
       printf("location =%d\n",find);// display that. the value increases itself.
   }
    return 0;
}
*/

program to use loop in enum data type

using codeblocks
----------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find;                    //variable for computer

int main()
{
    find=acer;                          // assigning position of value acer in set
   for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.

   {
       printf("location of =%d\n",find);// display that
   }
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find;                    //variable for computer

    for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.

   {
       printf("location =%d\n",find);// display that
   }
    return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
// we can set value inside of set. Then,the other values takes values in increasing order.
------------------------------------------------------------------------------
using turboc++
--------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find;                    //variable for computer

int main()
{
    find=acer;                          // assigning position of value acer in set
   for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.

   {
       printf("location of =%d\n",find);// display that
   }
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find;                    //variable for computer

    for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.

   {
       printf("location =%d\n",find);// display that
   }
    return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
// we can set value inside of set. Then,the other values takes values in increasing order. 

program to understand enum data type

using codeblocks
---------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find;                    //variable for computer

int main()
{
    find=acer;                          // assigning position of value acer in set
    printf("location of acer=%d\n",find);// display that
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find;                    //variable for computer

    find=acer;                          // assigning position of value acer in set
    printf("location of acer=%d\n",find);// display that
    return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
----------------------------------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find;                    //variable for computer

int main()
{
    find=acer;                          // assigning position of value acer in set
    printf("location of acer=%d\n",find);// display that
    return 0;
}

//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>

int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find;                    //variable for computer

    find=acer;                          // assigning position of value acer in set
    printf("location of acer=%d\n",find);// display that
    return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.

program to understand ftell() function

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
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET);                               // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR);                               //takes the current location of pointer.
                                                  //IF we take any other value than 0 ,
                                                   //it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END);                               //takes  position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k);                                         //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k);                            //closing of file.
getch();
return 0;
}
----------------------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------
/*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
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET);                               // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR);                               //takes the current location of pointer.
                                                  //IF we take any other value than 0 ,
                                                   //it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END);                               //takes  position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k);                                         //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k);                            //closing of file.
getch();
return 0;
}

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