C Program to Copy File

« Previous Program Next Program »

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

Things to do before Program

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

Let's suppose, here we have created a folder named c programs to the parent directory of Documents folder of my computer. Here is the snapshot of the folder named c programs

copy file directory c

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

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

this is

And create second file named cracker.txt without any content. Save both file in the same folder as shown in the snapshot given above, that is c programs folder. So the folder now looks like:

c copy file folder

If you open both the file, 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 codes.txt file to 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 file, that is, the source file and the target file. And then start reading the source file's content character by character and place or write the content of source file to target file on every read of character.

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

// Program to Copy File in C

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

This program was build and run under Code::Blocks IDE. Save the source code of above program in the same directory, that is inside the folder named c programs. To save it, use File->Save file as... navigation inside Code::Blocks. Here we have used codescracker.c as the name of source code file. After saving the source code of above program in the folder c programs present inside the Documents, the folder looks like:

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 first file, codes.txt (source file as created at early of this article inside the same folder where the source code belongs to). Press ENTER key, then again supply the name of another file say cracker.txt (the destination file, where the content of source.txt file is going to be copied). Finally press ENTER key to see the following output:

file copy program c

Here is final snapshot of the folder c programs:

copy file folder c program

Now if you will open the file cracker.txt (created with empty content at early of this article), then you will see the content of codes.txt file gets copied into it. Thats it. To learn more about file input and output operation in C, then refer to File Input/Output in C tutorial.

Same Program in Other Languages

« Previous Program Next Program »