bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / assignment-primary / assignment-p-15.c

assignment-p-15.c (1434B)


      1 /* Write a C program that reads a sequence of integers from a file named 'input.txt'. This
      2 program should segregate the odd numbers from the even numbers and store the odd
      3 numbers in a new file named 'ODDFile.txt' while storing the even numbers in another file
      4 named 'EVENFile.txt' */
      5 
      6 #include <stdio.h>
      7 
      8 #define FILENAME "input.txt"
      9 #define ODDFILE "ODDFile.txt"
     10 #define EVENFILE "EVENFile.txt"
     11 
     12 int main()
     13 {
     14     FILE *input = NULL;
     15     FILE *oddfile = NULL;
     16     FILE *evenfile = NULL;
     17     int num;
     18 
     19     input = fopen(FILENAME, "r");
     20     if (input == NULL)
     21     {
     22         printf("\nCould not open the file: %s", FILENAME);
     23         return 1;
     24     }
     25 
     26     oddfile = fopen(ODDFILE, "w");
     27     if (oddfile == NULL)
     28     {
     29         printf("\nCould not open the file: %s", ODDFILE);
     30         return 1;
     31     }
     32 
     33     evenfile = fopen(EVENFILE, "w");
     34     if (evenfile == NULL)
     35     {
     36         printf("\nCould not open the file: %s", EVENFILE);
     37         return 1;
     38     }
     39 
     40     while (fscanf(input, "%d", &num) == 1)
     41     {
     42         if (num % 2 == 0)
     43         {
     44             fprintf(evenfile, "%d ", num);
     45         }
     46         else
     47         {
     48             fprintf(oddfile, "%d ", num);
     49         }
     50     }
     51     
     52     printf("Successfully processed numbers from %s.\n", FILENAME);
     53     printf("Odd numbers written to %s.\n", ODDFILE);
     54     printf("Even numbers written to %s.\n", EVENFILE);
     55 
     56     fclose(input);
     57     fclose(oddfile);
     58     fclose(evenfile);
     59     
     60     return 0;
     61 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror