-->
Showing posts with label Program to store data/character in an opened file then read that character and print on screen in same program. program understand putc() and getc() in same program/file.. Show all posts
Showing posts with label Program to store data/character in an opened file then read that character and print on screen in same program. program understand putc() and getc() in same program/file.. Show all posts

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