Friday, November 12, 2010

C Progarm to input numbers into a file and separate the odd and even numbers into two different files

/*
#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *f1,*f2,*f3;
   int number,i;
   clrscr();
   printf(" \nContents of DATA file \n\n");
   f1=fopen("DATA","w");

   for(i=1;i<=30;i++)
   {
      scanf("%d",&number);

      if(number==-1)
      break;

      putw(number,f1);
   }
   fclose(f1);

   f1=fopen("DATA","r");
   f3=fopen("ODD","w");
   f2=fopen("EVEN","w");

   /* Read from DATA file */
   while((number=getw(f1))!=EOF)
   {
      if(number%2==0)
  putw(number,f2);
      else
  putw(number,f3);
   }

   fclose(f1);
   fclose(f2);
   fclose(f3);

   f2=fopen("ODD","r");
   f3=fopen("EVEN","r");

   printf("\n\n Contents of ODD FILE \n\n");
   while((number=getw(f2))!=EOF)
   printf("%4d",number);

   printf("\n\n Contents of EVEN FILE \n\n");
   while((number=getw(f3))!=EOF)
   printf("%4d",number);

   fclose(f2);
   fclose(f3);
   getch();
}


// Use -1 to stop inserting numbers.................................................

No comments:

Post a Comment