Some functions are used for file handling in C Language
Now, Algorithm of the programe
Step 1: Open a file pointer in write mode for craete a file of series of numbers (numbers.txt).
Step 2: read the numbers from keyboard and put into numbers.txt. press 0 to stop the series.
Step 3: Closed the said file pointer.
Step 4: Open the same file again but in read mode.
Step 5: Open another two file (odd.txt) and (even.txt) in write mode.
Step 6: Read the numbers from first file one by one and check it even or odd. If it is even the put in in even.txt otherwise put it into odd.txt
Step 7: Closed all file pointers using fcloseall() function.
#include<stdio.h>
void main()
{
FILE *num,*odd,*even;
int n=-1;
num=fopen("numbers.txt","w");
while(n!=0)
{
printf("Enter A Number (0 [Zero] to stop): ");
scanf("%d",&n);
if(n==0){break;}
fprintf(num,"%d \n",n);
}
fclose(num);
num=fopen("numbers.txt","r");
odd=fopen("odd.txt","w");
even=fopen("even.txt","w");
while(fscanf(num,"%d",&n)!=EOF)
{
if(n%2==0)
{
fprintf(even,"%d\n",n);
}
else
{
fprintf(odd,"%d\n",n);
}
}
fcloseall();
}
Data of number files (numbers.txt)
1
2
3
4
5
6
7
8
9
10
25
29
34
65
88
93
Data of odd files (odd.txt)
1
3
5
7
9
25
29
65
93
Data of odd files (even.txt)
2
4
6
8
10
34
88