Write a program code in C which creates a data file of integers and enters at least twelve integers through standard input in the file. Add a program code which reads these numbers .The program then should write all odd numbers to a file called ODD and all even numbers to a file called EVEN.
source file name: oddeven.C
#include<stdio.h>
int main()
{
int n;
FILE *f1,
*f2, *f3;
f1=fopen("data.txt","r");
f2=fopen("odd.txt","w");
f3=fopen("even.txt","w");
while(fscanf(f1,"%d",&n)!=EOF)
{
if(n%2==0)
fprintf(f3,"
%d",n);
else
fprintf(f2,"
%d",n);
}
//getch();
return 0;
}
Output available in following files:
data.txt
(12,45,-78,58,-456,36,79,96,-59,65,111,-178)
even.txt(12,-78,58,-456,36,96,-178)
odd.txt(45,79,-56,65,111)
0 Comments
Post a Comment