C program to copy contents from one file to another

Write a C program to copy contents from one file to another. Logic to copy contents from one file to another file in C program. How to copy contents from one file to another using functions in C programming.

Required knowledge

Basic Input Output, Pointers, Strings, File Handling

Logic to copy contents from one file to another

Step by step descriptive logic to copy file contents from one file to another.

  1. Input file path of source and destination file.
  2. Open source file in r (read) and destination file in w (write) mode.
  3. Read character from source file and write it to destination file using fputc().
  4. Repeat step 3 till source file has reached end.
  5. Close both source and destination file.

Program to copy one file to another

/**
 * C program to copy contents of one file to another.
 */

#include <stdio.h>
#include <stdlib.h>


int main()
{
    FILE *sourceFile;
    FILE *destFile;
    char sourcePath[100];
    char destPath[100];

    char ch;

    /* Input path of files to copy */
    printf("Enter source file path: ");
    scanf("%s", sourcePath);
    printf("Enter destination file path: ");
    scanf("%s", destPath);

    /* 
     * Open source file in 'r' and 
     * destination file in 'w' mode 
     */
    sourceFile  = fopen(sourcePath, "r");
    destFile    = fopen(destPath,   "w");

    /* fopen() return NULL if unable to open file in given mode. */
    if (sourceFile == NULL || destFile == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read/write privilege.\n");

        exit(EXIT_FAILURE);
    }


    /*
     * Copy file contents character by character.
     */
    ch = fgetc(sourceFile);
    while (ch != EOF)
    {
        /* Write to destination file */
        fputc(ch, destFile);

        /* Read next character from source file */
        ch = fgetc(sourceFile);
    }


    printf("\nFiles copied successfully.\n");


    /* Finally close files to release resources */
    fclose(sourceFile);
    fclose(destFile);

    return 0;
}

Note: You can also combine ch = fgetc(sourceFile) and EOF condition inside while loop. Hence above while loop to copy file can be written as.

while ((ch = fgetc(sourceFile)) != EOF)
    fputc(ch, destFile);

Program to copy one file to another using function

/**
 * C program to copy contents of one file to another using functions.
 */

#include <stdio.h>
#include <stdlib.h>


/* File copy function declaration */
int fcpy(FILE * sourceFile, FILE * destFile);


int main()
{
    FILE *sourceFile;
    FILE *destFile;
    char sourcePath[100];
    char destPath[100];

    int count;


    /* Input path of files to copy */
    printf("Enter source file path: ");
    scanf("%s", sourcePath);
    printf("Enter destination file path: ");
    scanf("%s", destPath);

    /* 
     * Open source file in 'r' and 
     * destination file in 'w' mode 
     */
    sourceFile  = fopen(sourcePath, "r");
    destFile    = fopen(destPath,   "w");

    /* fopen() return NULL if unable to open file in given mode. */
    if (sourceFile == NULL || destFile == NULL)
    {
        /* Unable to open file hence exit */
        printf("\nUnable to open file.\n");
        printf("Please check if file exists and you have read/write privilege.\n");

        exit(EXIT_FAILURE);
    }


    // Call function to copy file
    count = fcpy(sourceFile, destFile);
    printf("\nFiles copied successfully.\n");
    printf("%d characters copied.\n", count);


    /* Finally close files to release resources */
    fclose(sourceFile);
    fclose(destFile);

    return 0;
}



/**
 * Copy file contents character by charcter from 
 * one file to another. 
 * It return total character copied count.
 * 
 * @sourceFile  Pointer to source FILE.
 * @destFile    Pointer to destination FILE.
 */ 
int fcpy(FILE * sourceFile, FILE * destFile)
{
    int  count = 0;
    char ch;

    /* Copy file contents character by character. */
    while ((ch = fgetc(sourceFile)) != EOF)
    {
        fputc(ch, destFile);

        /* Increment character copied count */
        count++;
    }

    return count;
}

Suppose data\file2.txt exits in current workspace.

Output

Enter source file path: data\file2.txt
Enter destination file path: data\copy-file.txt

Files copied successfully.
142 characters copied.

Happy coding 😉