Read integers and store odd & even no. in a file
Program:
#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();
}
Output:
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
-
UpdatedDec 31, 2019
-
Views17,061
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order