Thursday, September 11, 2014

create a copy of a file using fgetc and fputc functions


Problem

Write a program that creates a copy of a file. The program first asks the user to input the "first file name" whose copy should be created and "second file" name for copy file. Make sure that file exist with the name "first file name" in the same folder where your C program is present. Use fgetc and fputc functions.

Solution - Code

#include<stdio.h>
#include<stdlib.h>

int main()
{
 char a[10],a1[10],ch;
 FILE *fp,*ft;

 printf ("Enter file name : ");
 gets (a);

 fp = fopen (a,"r");

 if (fp == NULL)
 {
  printf ("Not found");
  exit (0);
 }

 printf ("Enter file name : ");
 gets (a1);

 ft = fopen (a1,"w");
 if (ft == NULL)
 {
  printf ("Error in opening");
  exit (0);
 }

 while (1)
 {
  ch = fgetc(fp);
  if ( ch == EOF )
   break;
  else
   fputc (ch,ft);
  printf ("%c",ch);
 }

 fclose (fp);
 fclose (ft);

 system ("pause");
 return 0;
}

Sample Output

copy file using fgetc and fputc functions

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact