Write a C Program to read number from file and write them in odd and even file

A file named data contains series of integer numbers. Write a c program to read all numbers from file and then write all odd numbers into file named “odd” and write all even numbers into file named “even”. Display all the contents of these file on screen

In this program we will learn how to create a odd and even file also enter the related data into the particular file.

The c program to give step by step simple example. The file named DATA contains series of integer numbers. Code a program to read these numbers and the write all 'odd' numbers to a file to be called ODD and all 'even' numbers to a file to be called EVEN.

The program is shown below example. It uses three files simultaneously and therefor we need to define three-file pointers f1, f2, f3.

First, the file DATA containing integer values is created. The integer values are read from the terminal and are written to the fil DATA with the help of the statement putw(number, f1);

Notice the when we type we type-1, the reading is terminated and the file is closed. The next step is to open all the three files, DATA for reading, ODD and EVEN for writing. The contents of DATA file are read, integer by integer, by the function getw(f1) and written to ODD or EVEN file after an appropriate test. Note that the statement

(number = getw(f1))!= EOF

reads a value, assign the same to number, and then test for the end-of-file mark.

Finally, the program displays the contents of ODD and EVEN file. It is important to note that file ODD and EVEN opened for writing are closed before are reopened for reading.

  •  Program:

#include<stdio.h>

int main()
{
    FILE *f1,*f2,*f3;
    int number,i, n=10;

    printf("Contents of DATA file\n\n");

    f1 = fopen("DATA","w");
 
    for(i=0;i<n;i++)
    {
        scanf("%d",&number);
        if(number==-1)
        {
              break;
        }
        putw(number,f1);
    }
    fclose(f1);

    f1 = fopen("DATA","r");
    f2 = fopen("ODD","w");
    f3 = fopen("EVEN","w");

    while((number = getw(f1)) != EOF)
    {
        if(number%2==0)
   {
    putw(number,f3);
   }
        else
   {
    putw(number,f2);
   }
    }

    fclose(f1);
    fclose(f2);
    fclose(f3);

    f2 = fopen("ODD","r");
    f3 = fopen("EVEN","r");

    printf("\n\n Contents of ODD file \n\n");

    while((number = getw(f2)) != EOF)
  {
   printf("%d ",number);
  }


    printf("\n\nContents of EVEN file \n\n");

    while((number = getw(f3)) != EOF)
  {
   printf("%d ",number);
  }

    fclose(f2);
    fclose(f3);
    return 0;
}

  •  Output:

Contents of DATA file 11 22 33 44 55 66 77 88 99 00 12 34 56 78 90 -1 Contents of ODD file 11 33 55 77 99 Contents of EVEN file 22 44 66 88 0

  •  Visit:

Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 ) Here
Write a C program to find out distance travelled by the equation d=ut+at^2 Here
Write a C program to find that the accepted number is negative or positive or zero Here
Write a program to read mark of a student from keyboard the student is pass or fail ( using if else ) Here
Write a program to read three numbers from keyboard and find out maximum out of these three. (nested if else) Here
Write a program to check whether the entered character is capital, small letter, digit or any special character Here
Write a program to read marks from keyboard and your program should display equivalent grade according to following table (if else ladder) Here
Write a C program to prepare pay slip using following data Here
Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday Here
Write a C program to find out the Maximum and Minimum number from given 10 numbers Here
Write a C program to input an integer number and check the last digit of number is even or odd Here 
Write a C program to find factorial of a given number Here
Write a C program to reverse a number Here
Write a C program to generate first n number of Fibonacci series Here
Write a C program to find out sum of first and last digit of a given number Here
Write a C program to find the sum and average of different numbers Here
Write a program to calculate average and total of 5 students for 3 subjects Here
Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50 Here
Write a program to check whether the given number is prime or not Here
Write a program to evaluate the series 1^2+2^2+2+3^2+……+n^2 Here
Write a C program to find 1+1/2+1/3+1/4+....+1/n Here
Write a C program to find 1+1/2!+1/3!+1/4!+.....+1/n! Here
Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9! Here
Write a C program to read and store the roll no and marks of 20 students using array Here
Write a C program to find out which number is even or odd from list of 10 number using array Here
Write a program to find maximum element from 1-Dimensional array Here
Write a C program to calculate the average, geometric and harmonic mean of n elements in a array Here
Write a program to delete a character in given string Here
Write a program to replace a character in given string Here
Write a program to find a character from given string Here
Write a program to sort given array in ascending order Here
Write a program to reverse string Here
Write a program to convert string into upper case Here
Write a program that defines a function to add first n numbers Here
Write a function in the program to return 1 if number is prime otherwise return 0 Here
Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function Here
Write a C program to use recursive calls to evaluate F(x) = x – x3 / 3! + x5 / 5 ! – x7 / 7! + … xn/ n! Here
Write a program to find factorial of a number using recursion Here
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents Here
Write a program to read structure elements from keyboard Here

0 comments