-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort_Separate_into_Odd_Even.cpp
45 lines (42 loc) · 1.08 KB
/
Sort_Separate_into_Odd_Even.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*Example program for using getw and putw functions*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1, *f2, *f3;
int number, i;
printf("Enter the Contents (Numbers) of the DATA file (-1 to Stop) :: \n");
fopen_s(&f1, "DATA.txt", "w");
for (i = 1; i <= 30; i++)
{
scanf_s("%d", &number);
if (number == -1)
break;
_putw(number, f1);
}
fclose(f1);
fopen_s(&f1, "DATA.txt", "r");
fopen_s(&f2, "ODD.txt", "w");
fopen_s(&f3, "EVEN.txt", "w");
while ((number = _getw(f1)) != EOF) /* Read from data file*/
{
if (number % 2 == 0)
_putw(number, f3); /*Write to even file*/
else
_putw(number, f2); /*write to odd file*/
}
fclose(f1);
fclose(f2);
fclose(f3);
fopen_s(&f2, "ODD.txt", "r");
fopen_s(&f3, "EVEN.txt", "r");
printf("\n\nContents of the ODD file\n\n");
while ((number = _getw(f2)) != EOF)
printf("%4d ", number);
printf("\n\nContents of the EVEN file\n\n");
while ((number = _getw(f3)) != EOF)
printf("%4d ", number);
fclose(f2);
fclose(f3);
_getch();
}