A file named data contains series of integer no. Write a c program to read that no. and then write all odd no into file named odd no. and write all even no into file named even no. Display all the contents of these file on screen.
Introduction
Code
Practical-24.c
#include <stdio.h>
#include <stdlib.h>
void main()
{
int even;
int odd;
int num;
FILE *data;
data = fopen("Practical-24-Data", "r");
if (data == NULL)
{
printf("Error!");
exit(1);
}
FILE *ptrodd = fopen("24-odd.txt", "w+");
FILE *ptreven = fopen("24-even.txt", "w+");
while (fscanf(data, "%d", &num) > 0)
{
if (num % 2 == 0)
fprintf(ptreven, "%d\n", num);
else
fprintf(ptrodd, "%d\n", num);
}
fclose(ptrodd);
fclose(ptreven);
ptreven = fopen("24-even.txt", "r");
ptreven = fopen("24-odd.txt", "r");
printf("The even numbers are:\n");
while (fscanf(ptreven, "%d", &even) > 0)
printf("%d\n", even);
printf("\nThe odd numbers are:\n");
while (fscanf(ptrodd, "%d", &odd) > 0)
printf("%d\n", odd);
fclose(ptrodd);
fclose(ptreven);
return 0;
}
Output