C Program to create a sequential file containing integer numbers.

 Program to create a sequential file containing integer numbers. Read these numbers and write all odd numbers into a file ODD, and even numbers into a file EVEN. Display the contents from each.

#include<stdio.h>

main()

{

FILE *f1,*f2,*f3;

int n;

clrscr();

f1=fopen("NUM.DAT","w");

printf(" Enter series of integers\n");

do

{

scanf("%d",&n);

putw(n,f1);

}

while(n!=0);

fclose(f1);

f1=fopen("NUM.DAT","r");

f2=fopen("EVEN.DAT","w");

f3=fopen("ODD.DAT","w");

while((n=getw(f1))!=EOF)

{

if(n%2==0)

putw(n,f2);

else

putw(n,f3);

}

fclose(f1);

fclose(f2);

fclose(f3);

printf("\nContents of NUM.DAT");


f1=fopen("NUM.DAT","r");

while((n=getw(f1))!=EOF)

printf("%5d",n);

fclose(f1);

printf("\nContents of EVEN.DAT");

f2=fopen("EVEN.DAT","r");

while((n=getw(f2))!=EOF)

printf("%5d",n);

fclose(f2);

printf("\nContents of ODD.DAT");

f3=fopen("ODD.DAT","r");

while((n=getw(f3))!=EOF)

printf("%5d",n);

fclose(f3);

getch();

}

Post a Comment

Previous Post Next Post