PPS Practical 60

PPS Practical 60
  • Aim: 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.
  • Code:
    //For turbo C++ remove below comments
    #include <stdio.h>
    //#include<conio.h>
    void main()
    {
        // clrscr();
        FILE *fd,*fo,*fe;
        int n,i;
        fd=fopen("data.txt","w");
        printf("Contents of Data File : \n");
        for (i = 0; i < 100; i++){
            scanf("%d",&n);
            if (n==-1){
                break;
            }
            putw(n,fd);
        }
        fclose(fd);
        fd=fopen("data.txt","r");
        fo=fopen("odd.txt","w");
        fe=fopen("even.txt","w");
        while ((n=getw(fd))!=EOF){
            if (n%2==0){
                putw(n,fe);
            }
            else{
                putw(n,fo);
            }
        }
        fclose(fd);
        fclose(fo);
        fclose(fe);
        fe=fopen("even.txt","r");
        fo=fopen("odd.txt","r");
        printf("\nContents of odd file: \n");
        while ((n=getw(fo))!=EOF){
            printf("%4d",n);
        }
        // getch();
        printf("\nContents of even file: \n");
        while ((n=getw(fe))!=EOF){
            printf("%4d",n);
        }
        fclose(fe);
        fclose(fo);
        // getch();
    }
  • Output:
    Output of practical 60

Comments