Friday, May 30, 2014

FILE HANDLING USE OF FGETS & FPUTS

#include<conio.h>
#include<stdio.h>
void main()
{
  char str[30];
  FILE *fp1;
  clrscr();
  fp1=fopen("test2.txt","w");
  if(fp1==NULL)
  {
printf("file creation error\n");
getch();
exit();
  }
  else
  {
printf("enter the text\n");
while(gets(str)!=NULL)
{
  fputs(str,fp1);
  fputs("\n",fp1);
}
  }
  fclose(fp1);

  fp1=fopen("test2.txt","r");
  if(fp1==NULL)
     {
printf("file does not exist\n");
getch();
exit();
     }
  else
     {
while(fgets(str,30,fp1)!=NULL)
{
  puts(str);
}
     }
  fclose(fp1);
  getch();
}

No comments:

Post a Comment