I tried to make the following code.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
FILE *fp1,*fp2,*fp3;
int n,i,size;
printf("Enter no.of digits: ");
scanf("%d",&size);
fp1=fopen("NUMBER.txt","w");
printf("Enter the numbers: \n");
for(i=0;i<size;i++)
{
fflush(stdin);
scanf(" %d",&n);
fputc(n,fp1);
}
fclose(fp1);
fp1=fopen("NUMBER.txt","r");
fp2=fopen("EVEN.txt","w");
fp3=fopen("ODD.txt","w");
while((n=fgetc(fp1))!=EOF)
{
if(n%2==0)
fputc(n,fp2);
else
fputc(n,fp3);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp1=fopen("NUMBER.txt","r");
fp2=fopen("EVEN.txt","r");
fp3=fopen("ODD.txt","r");
printf("The content of number file are: ");
while((n=fgetc(fp1))!=EOF)
printf(" %d",n);
printf("\nThe content of even file are: ");
while((n=fgetc(fp2))!=EOF)
printf(" %d",n);
printf("\nThe content of odd file are: ");
while((n=fgetc(fp3))!=EOF)
printf(" %d",n);
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
The problem I face is that the contents of the files are in hex or binary. I want it to be readable with text editor not a hex editor. The other problem I face is the scanf() doesn't accept 3 digit numbers. The output is given below.
Enter no.of digits: 5 Enter the numbers: 123 34 456 67 789 The content of number file are: 123 34 200 67 21 The content of even file are: 34 200 The content of odd file are: 123 67 21
I tried to write the following code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1,*fp2,*fp3;
int size,n,a,b;
printf("Enter the size: ");
scanf("%d",&size);
fp1=fopen("numbers1.txt","w");
fp2=fopen("even1.txt","w");
fp3=fopen("odd1.txt","w");
printf("Enter the integers: ");
for(int i=0;i<size;i++)
{
scanf(" %d",&n);
//putc(n,fp1); reads as 12, 234 etc but digits in file is not visible
fprintf(fp1," %d\n",n);//reads 12, 234 as 12234 but digits in file are visible
}
fclose(fp1);
fp1=fopen("numbers1.txt","r");
n=getc(fp1);
while(n!=EOF)
{
if(n%2==0)
putc(n,fp2);
else if(n%2==1)
putc(n,fp3);
n=getc(fp1);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
}
In the above code the content in the files are in text, but the odd and even files read char by char. The contents of odd, even and number files are given blow.
odd file: 133577
even file: 2 4 46 6 8
Number file: 123 34 456 67 78
Please help me