Wednesday, 18 March 2015

Read integers and store odd & even no. in a file Using C


# include <stdio.h>
# include <conio.h>

void main()
{
FILE *fptr1, *fptr2, *fptr3 ;
int n, i, num ;
clrscr() ;
printf("Enter number of values : ") ;
scanf("%d", &n) ;
printf("\nEnter the values : ") ;
fptr1 = fopen("NUMBERS.DAT", "w") ;
for(i = 0 ; i < n ; i++)
{
scanf("%d", &num) ;
putw(num, fptr1) ;
}
fclose(fptr1) ;
fptr1 = fopen("NUMBERS.DAT", "r") ;
fptr2 = fopen("ODD.DAT", "w") ;
fptr3 = fopen("EVEN.DAT", "w") ;
while((num = getw(fptr1)) != EOF)
{
if(num % 2 == 0)
putw(num, fptr3) ;
else
putw(num, fptr2) ;
}
fclose(fptr1) ;
fclose(fptr2) ;
fclose(fptr3) ;
fptr2 = fopen("ODD.DAT", "r") ;
fptr3 = fopen("EVEN.DAT", "r") ;
printf("\nContents of ODD file is : ") ;
while((num = getw(fptr2)) != EOF)
printf("%d\t", num) ;
printf("\n\nContents of EVEN file is : ") ;
while((num = getw(fptr3)) != EOF)
printf("%d\t", num) ;
fclose(fptr2) ;
fclose(fptr3) ;
getch() ;
}


RUN 1 :
~~~~~~~
Enter number of values : 6
Enter the values : 11 22 33 44 55 66
Contents of ODD file is : 11 33 55
Contents of EVEN file is : 22 44 66

No comments:

Post a Comment