-->

program to store a number and to read that using putw and getw

using codeblocks
---------------------------------------------------------------------------------------------------------------
//using putw() to store data(one integer) and getw() to read (one integer)that data from an opened file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
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
        putw(i,p);
    }
    fclose(p);                              //closes the file
    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((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;
}
//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;
}
*/
-----------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//using putw() to store data(one integer) and getw() 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
        putw(i,p);
    }
    fclose(p);                              //closes the file
    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((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;
}
//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 store data/character in an opened file then read that character and print on screen in same program.

using codeblocks
----------------------------------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened //file
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file
    }
    fclose(p);                              //closes the file
    printf("data stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character 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;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/
------------------------------------------------------------------------------------------------------------
using turboc++
---------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened //file
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file
    }
    fclose(p);                              //closes the file
    printf("data stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
     getch();
    return 0;
}

Program to read data/character from opened file.

using codeblocks
----------------------------------------------------------------------------------
//using getc() to read (one character)that data from an opened
 #include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");           // openes the file in read only mode. We should know the file name.
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
-------------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------

//using getc() to read (one character)that data from an opened
 #include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");           // openes the file in read only mode. We should know the file name.
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
getch();
    return 0;
}

Program to store data/character in a datafile

using codeblocks
--------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened file
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file of pointer 'p'.
    }
    fclose(p);                              //closes the file
  }
-------------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened file
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file of pointer 'p'.
    }
    fclose(p);                              //closes the file
getch();
  }

program to find sum of two distances measured in feet and inch using function and typedef.

using codeblocks
------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.
-----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
#include<conio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
   getch();
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.


program to understand typedef

using codeblocks
------------------------------------------------------------------------------------------
//understanding typedef
#include <stdio.h>
int main()
{
    typedef int second_type;// creates another data type named second_type. It gets all property of 'int'
    second_type a,b,c;      // second_type is working as integer data type.
    printf("enter a,b,and c\n");
    scanf("%d%d%d",&a,&b,&c);  //gets input
    printf("a=%d,b=%d and c=%d",a,b,c);//prints output
    return 0;
}

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

//understanding typedef
#include <stdio.h>
#include<conio.h>
int main()
{
    typedef int second_type;// creates another data type named second_type. It gets all property of 'int'
    second_type a,b,c;      // second_type is working as integer data type.
    printf("enter a,b,and c\n");
    scanf("%d%d%d",&a,&b,&c);  //gets input
    printf("a=%d,b=%d and c=%d",a,b,c);//prints output
   getch(); 
  return 0;
}

Program to understand typedef for struct.

using codeblocks
----------------------------------------------------------------------------------------
//understanding typedef with struct
#include <stdio.h>
typedef struct                   // typedef for struct to assign its property to access
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    return 0;
}
-------------------------------------------------------------------------------------
Or
--------------------------------------------------------------------------------
//understanding typedef with struct
#include <stdio.h>
typedef struct   tag                // typedef for struct to assign its property to access via 'tag'
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    return 0;
}

//note: putting a tag doesnot make any different. We have to use same method as given in above //example.
//Mostly, the tag is unimportant.

---------------------------------------------------------------------------------------------------
using turboC++
-------------------------------------------------------------------------

//understanding typedef with struct
#include <stdio.h>
#include<conio.h>
typedef struct                   // typedef for struct to assign its property to access
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    getch();
    return 0;
}