-->

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

No comments:

Post a Comment