maheswar02.c
Source Code
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE *fIn, *fEven, *fOdd;
int n;
fIn = fopen("input.txt", "r");
fEven = fopen("EVENFile.txt", "w");
fOdd = fopen("ODDFile.txt", "w");
// Check if files opened successfully
if ((fIn == NULL) || (fEven == NULL) || (fOdd == NULL)) {
printf("ERROR : one or more file opening FAILED!");
if (fIn != NULL) fclose(fIn);
if (fEven != NULL) fclose(fEven);
if (fOdd != NULL) fclose(fOdd);
exit(1);
}
// Read numbers and separate them into even and odd files
while (fscanf(fIn, "%d", &n) != EOF)
(n % 2 == 0) ? fprintf(fEven, "%d\n", n) : fprintf(fOdd, "%d\n", n);
// Close all files
fclose(fIn);
fclose(fEven);
fclose(fOdd);
printf("OPERATION COMPLETED SUCCESSFULLY.");
return 0;
}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
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