maheswar02.c (839B)
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 int main() { 5 FILE *fIn, *fEven, *fOdd; 6 int n; 7 8 fIn = fopen("input.txt", "r"); 9 fEven = fopen("EVENFile.txt", "w"); 10 fOdd = fopen("ODDFile.txt", "w"); 11 12 // Check if files opened successfully 13 if ((fIn == NULL) || (fEven == NULL) || (fOdd == NULL)) { 14 printf("ERROR : one or more file opening FAILED!"); 15 if (fIn != NULL) fclose(fIn); 16 if (fEven != NULL) fclose(fEven); 17 if (fOdd != NULL) fclose(fOdd); 18 exit(1); 19 } 20 21 // Read numbers and separate them into even and odd files 22 while (fscanf(fIn, "%d", &n) != EOF) 23 (n % 2 == 0) ? fprintf(fEven, "%d\n", n) : fprintf(fOdd, "%d\n", n); 24 25 // Close all files 26 fclose(fIn); 27 fclose(fEven); 28 fclose(fOdd); 29 30 printf("OPERATION COMPLETED SUCCESSFULLY."); 31 return 0; 32 }