C program to copy a file

In this article, you will learn and get code for copying the content of one file to another in the C language. But before going through the program given below, let's first understand the things that have to be done before performing the copy file operation in C.

Things to do before the program

Before going to the program, let's first do the following things:

Let's suppose we have created a folder named "c programs" in the parent directory of the Documents folder on my computer. Here is a snapshot of the folder named "c programs."

copy file directory c

Because this is a newly created folder, So the folder is empty for now. But I'm going to include some files related to file copying programs.

Now it's time to create a file named codes.txt with the following content.

this is codescracker.com

and create a second file named cracker.txt without any content. Save both files in the same folder as shown in the snapshot given above, which is the "c programs" folder. So the folder now looks like this:

c copy file folder

If you open both files, then you will see some content present inside the first file, codes.txt, whereas the second file, cracker.txt, is empty. Now let's move on to the program to copy the content of the codes.txt file to the cracker.txt file.

Program to Copy One File to Another in C

To copy the content of one file to another in C programming, you have to first open both the source file and the target file. And then start reading the source file's content character by character, placing or writing the content of the source file to the target file on every character read.

The question is: write a program in C that copies the content of one file to another file. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch, fileName1[20], fileName2[20];
    FILE *fs, *ft;
    printf("Enter Source File Name (with extension): ");
    gets(fileName1);
    fs = fopen(fileName1, "r");
    if(fs == NULL)
    {
        printf("\nError in Opening the file, %s", fileName1);
        getch();
        return 0;
    }
    printf("Enter Target File Name (with extension): ");
    gets(fileName2);
    ft = fopen(fileName2, "w");
    if(ft == NULL)
    {
        printf("\nError in Opening the file, %s", fileName2);
        getch();
        return 0;
    }
    ch = fgetc(fs);
    while(ch != EOF)
    {
        fputc(ch, ft);
        ch = fgetc(fs);
    }
    printf("\nFile copied successfully.");
    fclose(fs);
    fclose(ft);
    getch();
    return 0;
}

This program was built and runs in the Code::Blocks IDE. Save the source code of the above program in the same directory, which is inside the folder named "c programs." To save it, use File->Save file as... navigation inside Code::Blocks. The source code file is named codescracker.c in this case. After saving the source code of the preceding program in the Documents folder "c programs," the folder looks like this:

copy file to another c

Now it's time to build and run the above program. Here is the sample run:

c program copy file

Now supply the name of the first file, codes.txt (the source file as created at the beginning of this article, inside the same folder where the source code belongs). Press the Enter key, then again supply the name of another file, say cracker.txt (the destination file, where the content of the source.txt file is going to be copied). Finally, press the ENTER key to see the following output:

file copy program c

Here is the final snapshot of the folder "c programs"

copy file folder c program

If you open the file cracker.txt (which was created with empty content at the start of this article), you will see that the contents of the codes.txt file have been copied into it.

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this article? Share it!






Like/follow us on social media for updates!