Wednesday, 7 March 2012

File handling programme in c read six input from user and write even and odd no. to different file



  /*even and odd no.using file handelling*/
 #include<stdio.h>
void main()
 {
    FILE *f1,*f2,*f3;
    int number,i;
    printf("contents of data file\n\n");
    f1=fopen("data","w");
    for(i=0;i<=6;i++)//enter six values 
      {
        scanf("%d",&number);//read from user
         if(number==-1)
         break;
         putw(number,f1);//write data into file 1(source,destination)
       }
 
  fclose(f1);//close file 1
    f1=fopen("data","r");//open file in read mode
    f2=fopen("odd","w");
    f3=fopen("even","w");
    while((number=getw(f1))!=EOF)//logic for even n odd numbers
     {                                             //loop till end of file
        if(number%2==0)
          {
            putw(number,f3);
           }
        else
          {
           putw(number,f2);
          }
    }
   fclose(f1);//close all three file
   fclose(f2);
   fclose(f3);
   f2=fopen("odd","r");//open both files in read mode
   f3=fopen("even","r");
   printf("\n\ncontents of odd file\n\n");//display content of odd no. file
   while((number=getw(f2))!=EOF)
   printf("%4d",number);
   printf("\n\ncontentsof even file\n\n");//diplay content of even no. file
   while((number=getw(f3))!=EOF)
   printf("%4d",number);
   fclose(f2);
   fclose(f3);
 }

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}