-->

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

No comments:

Post a Comment