C Program to Copy Files Content From One to Other

In this tutorial you will learn about the C Program to Copy Files Content From One to Other and its application with practical example.

In this tutorial, we will learn to create c program to copy files In C programming language. Copying of two file means the contents of one file copied into the other file.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics: C Pointers, C File Handling, C Strings and C Input Output.

Create required files to merge two files

Let’s first create files (inside the current project directory) as following:

  • f1.txt – first source file
  • f2.txt – target file

All of the above files must be saved in the folder where you are saving program file. Put the following content in corresponding file:

f1.txt

How to Copy Files Into Other File?

In this C program we will copy file (f1.txt) contents into other file (f2.txt). We will open source file in “read” mode and target file in “write” mode. Then the content of the Source file (f1.txt) will be copied character by character into target file(f2.txt). The file will be read until their EOF (end of file) is reached.

C Program to Copy Files Content From One to Other

In this program we will copy one file contents into other file . We would first declared and initialized the required variables. Next, we would prompt user to input source and target file names(f1.txt and f2.txt) respectively. Later in the program we will copy file (f1.txt) contents into other file (f2.txt) using c file handling functions. Now, when you open the file (f2.txt) you will see that the content of file (f1.txt) is copied into it.

Output:-

C-Program-to-Copy-Files-Content-From-One-to-Other

In the above program, we have first declared and initialized a set variables required in the program.

  • *source= file pointer for first source file
  • *target= file pointer for second source file
  • source_file = to hold first source file name
  • target_file = to hold second source file name
  • ch = to read character

Now, user needs to provide the source file and target name. We will assign it to variable ‘source_file’ and ‘target_file’ respectively.

Next, we will try to open source and target files using fopen() function. In case, if file doesn’t exist or you don’t have the required  permission, then the function fopen() will returns NULL. Therefore, before starting the operation, we would have to check whether it returns NULL or not. If it returns NULL, then we will print an error message, otherwise will continue with the copy operation.

Now we will read through the character by character using fgetc() function, and will assign to FILE pointer source one by one until it return EOF (End Of File). And using fputc() function, we will put the character into the target FILE pointer. This way we get characters one by one from source file and put into the target file. Now, if you open the file f2.txt, you will see that the content of file(f1.txt) is copied into it.

In this tutorial we have learn about the C Program to Copy Files Content From One to Other and its application with practical example. I hope you will like this tutorial.