C Programming Tutorial

Advertisements

Write a program to copy all even numbers from one file to another in C



       #include<stdio.h>
       #include<conio.h>
       #include<process.h>

       void main()
       {
              FILE *fp1,*fp2;
              int num;
              clrscr();

              fp1 = fopen("file.txt","r");
              fp2 = fopen("file.txt","wt");
              
              if(fp == NULL)
              {
                     printf("\nCan't open file or file doesn't exist."); 
                     exit(0);
              }

              printf("\nData in file...\n");
              
              while((num = getw(fp1))!=EOF)
              {
                  if(num % 2 == 0)
                  putw(num,fp2);
               }
               
               fseek(fp2,0,SEEK_SET);

              while((num = getw(fp2))!=EOF)
                  printf("\n%d",num);
                                                
              fclose(fp1);               
              fclose(fp2);               

              getch();
       }




Advertisement