C Sample File : File Handling



File Handling :


// Program To Write Text Into A File Through File Handling

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
     FILE *fp;
     char str[100];
     clrscr();
     fp=fopen("dr.txt","w");
     if(fp==NULL)
     {
          printf("Cannot Open The File !!!");
     }
     printf("Enter Text To Write in dr.txt File : \n\n");
     while(strlen(gets(str))>0)
     {
          fputs(str,fp);
          fputs("\n",fp);
     }
     fclose(fp);
     getch();
}




// Program To Read Text From A File Through File Handling

#include<stdio.h>
#include<conio.h>
void main()
{
     FILE *fp;
     char ch;
     clrscr();
     fp=fopen("dr.txt","r");
     printf("\nContents Of dr.txt File Are : \n\n");
     while(1)
     {
          ch=fgetc(fp);
          if(ch==EOF)
          {
              break;
          }
          printf("%c",ch);
     }
     fclose(fp);
     getch();
}

Comments

Popular Posts