Friday, May 30, 2014

FILE HANDLING USE OF FPUTW & FGETW

 #include<conio.h>
 #include<stdio.h>
 void main()
 {
  FILE *fp1;
  int i,n,val;
  clrscr();
  fp1=fopen("num1.dat","wb");
  if(fp1==NULL)
  {
     printf("file creation error\n");
     exit();
  }
 else
 {
     printf("enter the no of values\n");
     scanf("%d",&n);
     printf("enter the values\n");
     for(i=1;i<=n;i++)
     {
scanf("%d",&val);
putw(val,fp1);
     }
 }
 fclose(fp1);

 fp1=fopen("num1.dat","rb");
 if(fp1==NULL)
   {
printf("file does not exist\n");
getch();
exit();
   }
 else
   {
val=getw(fp1);
while(!feof(fp1))  //this will check for real end of file
//while(val!=EOF)  //this will stop reading as it get val=-1 even before real EOF
{
   printf("\n%d",val);
   val=getw(fp1);
}
   }
 fclose(fp1);
 getch();
 }

No comments:

Post a Comment