bsc

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

assignment-p-15.c (1625B)


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