C Program to Implement File Operations
This is the C Program to Implement File Operations it opens a file with numbers in read mode and create separate text files for even and odd numbers(write mode)
#include<stdio.h>
main()
{
FILE *fp1, *fp2, *fp3;
int num,i;
printf("Input Numbers 0 to Terminate n");
fp1=fopen("DATA", "w");
for(i=0;i<31;i++)
{
scanf("%d",&num);
putw(num,fp1);
if(num==-1)
{break;}
}
fclose(fp1);
fp1=fopen("DATA", "r");
fp2=fopen("EVEN", "w");
fp3=fopen("ODD", "w");
while((num=getw(fp1))!=EOF)
{
if(num%2==0){
putw(num,fp2);}
else
putw(num,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp2=fopen("EVEN", "r");
printf("EVEN Numbers n");
printf("n");
while((num=getw(fp2))!=EOF)
{
printf("%4dn",num);
}
fclose(fp2);
fp3=fopen("ODD", "r");
printf("ODD Numbers nn");
while((num=getw(fp3))!=EOF)
{
printf("%4dn",num);
}
fclose(fp3);
}
Recent Comments