getc() and putc() in C

getc() function in c

Function getc() reads a single character from the file

putc function in c

putc() function writes a character to the file identified by its second argument

getc(in_file);
putc(c, out_file);

Example of getc() and putc() function

#include <stdio.h>
main ()

 char in_file[30], out_file[30];
 FILE *fpin, *fpout;
 int c;
 printf("This program copies the source file to the destination
file
\n\n");
 printf("Enter name of the source file :");
 scanf("%30s", in_file);
 printf("Enter name of the destination file :");
 scanf("%30s", out_file);
 if((fpin=fopen(in_file, "r")) == NULL)
 printf("Error could not open source file for
reading\n");
 else if ((fpout=fopen(out_file, "w")) == NULL)
 printf("Error could not open destination file for
reading\n");
 else
 {
 while((c =getc(fpin)) != EOF)
 putc(c, fpout);
 printf("Destination file has been copied\n");
 }
}

Leave a Comment