C program to read any file and put odd and even Numbers in separate files.

C Program



#include<stdio.h>
#include<conio.h>
int main()
{
    FILE *f1,*f2,*f3;
    int number,i;
    printf("\n Contents of DATA file:");
    f1 = fopen("DATA.txt","w");
for(i=1;i<=30;i++)
    {
        scanf("%d",&number);
        if(number==-1) break;
        putw(number,f1);
    }
    fclose(f1);

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

    while((number=getw(f1))!= EOF)
    {
        if(number%2==0)
            putw(number,f3);
        else
            putw(number,f2);
    }
    fclose(f1);
    fclose(f2);
    fclose(f3);

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

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

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

    fclose(f2);
    fclose(f3);
    return 0;
  }


INPUT && OUTPUT





Contain of Odd.txt File



Odd File



Contain of Even.txt File



Even File Contain.





So As you can see clearly all the odd values we entered were stored in Odd.txt File. and  Similarly  all even values we entered were stored in Even.txt File. We got our desired Output.

     

Post a Comment

0 Comments