/* * Program to read numbers from a file and write to two files. * * Input: text file "numbers.txt" containing integers. * * Output: text files "odds.txt", "evens.txt" containing odd and * even numbers, respectively, from "numbers.txt". */ #include #include int main(void) { FILE * nums; FILE * odds; FILE * evens; int input; /* open input file and check result */ nums = fopen("numbers.txt", "r"); if (nums == NULL) { printf("unable to open input file\n"); exit(1); } /* open output files and check result */ odds = fopen("odds.txt", "w"); evens = fopen("evens.txt", "w"); if ((odds == NULL) || (evens == NULL)) { printf("unable to open output file\n"); exit(1); } /* read numbers from input file and copy to output files */ while (fscanf(nums, "%d", &input) == 1) { if ((input % 2) == 0) { fprintf(evens, "%d\n", input); } else { fprintf(odds, "%d\n", input); } } /* close files */ fclose(nums); fclose(odds); fclose(evens); printf("Goodbye!\n"); return 0; }