Discussion Forums

New User? Please read instructions!

C program to copy content of one file into another  

  RSS

thiyagarajan
Eminent Member
Joined: 8 months  ago
Posts: 26
27/01/2017 9:40 am  

C program how to copy content of one file into another


ReplyQuote
admin
Eminent Member
Joined: 5 years  ago
Posts: 26
01/02/2017 5:39 am  

Hi Thiyagarajan,

You can use below C program to copy the content of one file into another. If you want to add error handling on file operation such as fopen(), fclose(), please refer  http://fresh2refresh.com/c-programming/c-file-handling/fopen-fclose-gets-fputs-functions-c/ for better understanding.

 

#include <stdio.h>

int main()

{

   char ch;

   FILE *fp1;

   FILE *fp2;

   /* Assume this test1.c file has some data.

      For example “Hi, How are you?” */

   if (fp1 = fopen("test1.c", "r"))

   {

      ch = getc(fp1);

      // Assume this test2.c file is empty

      fp2 = fopen("test2.c", "w+")

      while (ch != EOF)

      {

         fputc(ch, fp2);

         ch = getc(fp1);

      }

      fclose(fp1);

      fclose(fp2);

      return 0;

   }

   return 1;

}

 
Edited: 8 months  ago

ReplyQuote
  
Working

Please Login or Register

1. Online Compilers - Compile & Run your C/C++ programs:

✅ Online Compilers

2. Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!