A C program to separate even and odd to another two files
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f,*od,*ev;
int num,i;
clrscr();
printf("Enter numbers,and -1 for stop\n");
f=fopen("num.dat","w");
for(i=1;i>0;i++)
{
scanf("%d",&num);
if(num==-1)
break;
putw(num,f);
}
fclose(f);
f=fopen("num.dat","r");
od=fopen("odd.dat","w");
ev=fopen("even.dat","w");
while((num=getw(f))!=EOF)
{
if(num%2==0)
putw(num,ev);
else
putw(num,od);
}
fclose(f);
fclose(od);
fclose(ev);
od=fopen("odd.dat","r");
ev=fopen("even.dat","r");
printf("The contents of odd file is\n\n");
while((num=getw(od))!=EOF)
printf("%d ",num);
printf("\n\n");
printf("The contents of EVEN file is\n\n");
while((num=getw(ev))!=EOF)
printf("%d ",num);
fclose(od);
fclose(ev);
getch();
}
|