C Program to Copy the Contents of One File into Another File

Introduction

In this article, I am going to write a c program to copy the contents of one file into another file. Here, we need two files. One file is the source file and another is a destination file. What does this mean? We will read the contents from the source file and write these contents in the destination file.

Also read: C Language Program to Count the Number of Lowercase Letters in a Text File

Logic of the C Program to Copy the Contents

We have to perform the following steps before writing a c program to copy the contents of one file into another file.

  1. Create a source file in reading mode.
  2. Create a destination file in writing mode.
  3. Check if is there any error while opening these files. If there is any error, then exit from the program.
  4. If we have come at this step, that means we have successfully open both the files.
  5. Now, we will read a source file character by character and at the same time, we will write this character into the destination file.
  6. After this reading and writing, we will close both files.

Also read: Program in C to Replace Capital C with Capital S in a File

Now, in the next paragraph, you can find our actual program which is reading a file and writing its contents into another file.

C program to copy the contents of one file into another file

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp1,*fp2;
    char ch;
    fp1=fopen("abc.txt","r");
    fp2=fopen("xyz.txt","w");
    if(fp1==NULL)
    {
        printf("Error while opening a file for reading");
        return 0;
    }
    if(fp2==NULL)
    {
        printf("Error while opening a file for reading");
        return 0;
    }
    while((ch=fgetc(fp1))!=EOF)
    {
        fputc(ch,fp2);
    }
    printf("File is Successfully Copied");
    fclose(fp1);
    fclose(fp1);
    return 0;
}

Detailed Explanation

Now, I am going to explain to you step by step explanation of the above program.

FILE *fp1,*fp2;

Using this statement, we are declaring two file pointers that will point to the actual files. Whenever we deal with the file handing programs in c, we create file pointers. Here, we are dealing with two files, therefore, we have created two file pointers i.e. fp1 and fp2.

char ch;

Here, ch is the character variable. We are using this variable to store the character which we are going to read from the source file and writing this same character into the destination file.

fp1=fopen(“abc.txt”,”r”);

This statement is opening a file abc.txt in reading mode. You must already have this file before opening it, otherwise, it will show an error.

Also read: C Program to Remove White Spaces and Comments from a File

fp2=fopen(“xyz.txt”,”w”);

Here, we are opening xyz.txt in writing mode. This statement will open an existing file or create a new one if not available.

if(fp1==NULL) and if(fp2==NULL)

If there is any error while opening a file, the value of the file pointer becomes NULL. That is why we are using if statement here to verify the errors. If this condition becomes true, then we will not be able to proceed further and our program is terminated here. But, if there is no error, then we can perform our operation of copying the contents of one file into another file.

while((ch=fgetc(fp1))!=EOF)

This is the while loop. Here, fgetc(fp1) is reading a single character from a file that is associated with fp1 i.e. xyz.txt and storing that character in the variable ch. After that, we are comparing the value of ch with EOF means End Of File. If the value of ch is not equal to the EOF, then we will execute the loop, otherwise while loop will be terminated. That means the copying operation is successful.

But remember, fgetc() will read a single character only at one time and after reading that character, it will move to the next one. That is why we are using a while loop here because we don’t know how many characters we will have to read.

fputc(ch,fp2);

This statement is written inside the body of the while loop. If we are in this block, that means we have read a character from the file. Now, it is time to write the same character in the destination file i.e. xyz.txt. But, how would we do this?

For this, the fputc() function is there. This function will write the value of ch into the file which is associated with the file pointer fp2 and that file is xyz.txt in this program. We will be able to write this character into the file as long as the while loop is executed.

I don’t think, there is another better explanation for the c program to copy contents of one file into another file. But, maybe I am wrong. So, I will definitely appreciate it if you find any misunderstood things in this program. Please, feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment