C File Handling Programs

Question:12
Write a function in C named “copy_file()” to take input for source and target file name. further copy the contents.

#include<stdio.h>
 #include<conio.h>
 void copy_file()
 {
 FILE *fp,*fp1;
 char sfile[10],tfile[10];
 char ch;
 clrscr();
 printf("Enter the source file name ");
 scanf("%s",sfile);
 printf("Enter the target file name ");
 scanf("%s",tfile);
  
 fp=fopen(sfile,"r");
 fp1=fopen(tfile,"w");
  
 while((ch=getc(fp))!=EOF)
 {
 putc(ch,fp1);
 }
 fclose(fp);
 fclose(fp1);
 printf("The contents of the file %s are \n",sfile);
 fp=fopen(sfile,"r");
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp);
 printf("The contents of copied  file %s are \n",tfile);
 fp=fopen(tfile,"r");
 while((ch=getc(fp))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp);
 }
int main()
 {
   copy_file ();
   return(0);
 } 

Question:13
Write a function in C named “transfer()” to read the contents of a file and further transfer
all alphabets to a file named: alpha
all upper case to a file named: upper
all lower case to a file named: lower
all digits to a file named: digit

#include<stdio.h>
 #include<conio.h>
 #include<ctype.h>
 void transfer()
 {
 FILE *fp1,*fp2,*fp3,*fp4,*fp5;
 char ch,source[15];
 printf("Enter source file name ");
 scanf("%s",source);
 fp1=fopen(source,"r");
 fp2=fopen("alpha","w");
 fp3=fopen("lower","w");
 fp4=fopen("upper","w");
 fp5=fopen("digit","w");
  
 while((ch=getc(fp1))!=EOF)
 {
 if (isalpha(ch))
 putc(ch,fp2);
 if (islower(ch))
 putc(ch,fp3);
 if (isupper(ch))
 putc(ch,fp4);
 if (isdigit(ch))
 putc(ch,fp5);
 }
 fclose(fp1);
 fclose(fp2);
 fclose(fp3);
 fclose(fp4);
 fclose(fp5);
 printf("\nThe contents of the file data\n");
 fp1=fopen("data","r");
 while((ch=getc(fp1))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp1);
  
 printf("\nThe contents of the file alpha\n");
 fp1=fopen("alpha","r");
 while((ch=getc(fp1))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp1);
  
 printf("\nThe contents of the file lower\n");
 fp1=fopen("lower","r");
 while((ch=getc(fp1))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp1);
  
 printf("\nThe contents of the file upper\n");
 fp1=fopen("upper","r");
 while((ch=getc(fp1))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp1);
  
 printf("\nThe contents of the file digit\n");
 fp1=fopen("digit","r");
 while((ch=getc(fp1))!=EOF)
 {
 printf("%c",ch);
 }
 fclose(fp1);
 }
  
int main()
 {
   transfer();
   return(0);
 } 

Question:14
Write a C program to create a file store numbers into it and further display the contents?

#include<stdio.h>
 #include<conio.h>
int main()
 {
 FILE *fp;
 int n;
 fp=fopen("num.dat","w");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return(0);
 }
 while(1)//for(;;)
 {
 printf("Enter number to add in file,to end press 0 (zero) ");
 scanf("%d",&n);
 if (n==0)
 break;
 putw(n,fp);
 }
 fclose(fp);
 fp=fopen("num.dat","r");
 if(fp==NULL)
 {
   printf("Unable to open the file\n");
   getch();
   return(0);
 }
 while((n=getw(fp))!=EOF)
 {
 printf("%d\n",n);
 }
 fclose(fp);
 return(0);
 } 

Question : 15
Write a C program to create a file store numbers into it and further:
(i).copy all even numbers to a file named “even”
(ii)copy all odd numbers to a file named “odd”
further display the contents of all the files.

#include<stdio.h>
 #include<conio.h>
int main()
 {
 FILE *fp,*fp1,*fp2;
 int n;
 fp=fopen("num.dat","w");
 while(1)//for(;;)
 {
 printf("Enter number to add in file, to end press 0 (zero) ");
 scanf("%d",&n);
 if (n==0)
 break;
 putw(n,fp);
 }
 fclose(fp);
 fp=fopen("num.dat","r");
 fp1=fopen("even","w");
 fp2=fopen("odd","w");
 while((n=getw(fp))!=EOF)
 {
 if (n%2==0)
 putw(n,fp1);
 else
 putw(n,fp2);
 }
 fclose(fp);
 fclose(fp1);
 fclose(fp2);
 printf("The contents of the file num.dat are \n");
 fp=fopen("num.dat","r");
 while((n=getw(fp))!=EOF)
 {
 printf("%d\n",n);
 }
 fclose(fp);
 printf("The contents of the file even are \n");
 fp=fopen("even","r");
 while((n=getw(fp))!=EOF)
 {
 printf("%d\n",n);
 }
 fclose(fp);
 printf("The contents of the file odd are \n");
 fp=fopen("odd","r");
 while((n=getw(fp))!=EOF)
 {
 printf("%d\n",n);
 }
 fclose(fp);
 return(0);
 }