- Asked 2/16
- 2074 Asoj · 4 marks
- 2069 Chaitra · 8 marks
Odd and even numbers to separate files
Short answer
Read integers until user says 'no', writing all to DATA file. Then reopen DATA for reading, and create odd.txt and even.txt. Read each integer, write to odd.txt if odd, else even.txt. Handle file open errors and invalid input.
Write a program in C to read integers from user until user says "no". After reading the data write all the odd numbers to a file named odd.txt and all the even numbers to file named even.txt. (Take care of all possible error conditions to open a new file named DATA.)
Logic.
- Create the file
DATAin write mode; iffopen()returnsNULL, print an error and stop. - Read integers in a loop, write each into
DATA, and ask "Continue (yes/no)?" until the user typesno. Invalid input stops the loop. - Close
DATA, open it again for reading and openodd.txtandeven.txtfor writing, checking everyfopen()forNULL. - Read each number from
DATAwithfscanf(); write it toodd.txtifx % 2 != 0, otherwise toeven.txt. - Close all files.
Error conditions handled: failure of fopen() for each file, and input that is not an integer.
#include <stdio.h>
#include <string.h>
int main(void)
{
FILE *fd, *fo, *fe;
int n, x;
char ans[10];
fd = fopen("DATA", "w"); /* new file DATA for all numbers */
if (fd == NULL) {
printf("Cannot create DATA\n");
return 1;
}
do {
printf("Enter an integer: ");
if (scanf("%d", &n) != 1) { /* invalid input */
printf("Invalid input\n");
break;
}
fprintf(fd, "%d\n", n);
printf("Continue (yes/no)? ");
scanf("%9s", ans);
} while (strcmp(ans, "no") != 0);
fclose(fd);
fd = fopen("DATA", "r");
fo = fopen("odd.txt", "w");
fe = fopen("even.txt", "w");
if (fd == NULL || fo == NULL || fe == NULL) {
printf("File opening error\n");
return 1;
}
while (fscanf(fd, "%d", &x) == 1) {
if (x % 2 != 0)
fprintf(fo, "%d\n", x);
else
fprintf(fe, "%d\n", x);
}
fclose(fd);
fclose(fo);
fclose(fe);
printf("Done. See odd.txt and even.txt\n");
return 0;
}
Sample run (numbers 7, 12, 5, 8, 21)
Input: 7 yes 12 yes 5 yes 8 yes 21 no
Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Enter an integer: Continue (yes/no)? Done. See odd.txt and even.txt
odd.txt contains 7, 5, 21 and even.txt contains 12, 8.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Count even and odd until -1 · Computer Programming, chapter 5
- Read records until end; show nth record · Computer Programming, chapter 10 · asked 2×
- RECORD.txt item cost with VAT · Computer Programming, chapter 10
- Copy contents of one file to another · Computer Programming, chapter 10 · asked 2×
- I/O functions used with data files · Computer Programming, chapter 10 · asked 2×
- Count prime and composite until -1 · Computer Programming, chapter 5
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Student file read/write and modify by roll · Object Oriented Programming, chapter 8
- Even and odd parts of a signal · Data Communication, chapter 2
- 8086 ALP: sum of 100 even numbers · Microprocessor, chapter 3