C Program for copying the contents of one file into another file

File copying is a fundamental file operation in C programming that involves reading data from one file and writing it to another file. This process requires proper file handling using standard C library functions for opening, reading, writing, and closing files.

Syntax

FILE *fopen(const char *filename, const char *mode);
int fgetc(FILE *stream);
int fputc(int character, FILE *stream);
int fclose(FILE *stream);

Parameters

  • filename − Path to the file to be opened
  • mode − File access mode ("r" for reading, "w" for writing)
  • stream − Pointer to FILE object
  • character − Character to be written to the file

Example: File Copy Using Character-by-Character Method

This program demonstrates copying contents from one file to another using fgetc() and fputc() functions ?

Installation: Create two text files in your working directory: "source.txt" (with some content) and ensure "destination.txt" can be created.

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

int main() {
    FILE *source, *destination;
    char ch;
    
    /* Open source file for reading */
    source = fopen("source.txt", "r");
    if (source == NULL) {
        printf("Error: Cannot open source file!<br>");
        return 1;
    }
    
    /* Open destination file for writing */
    destination = fopen("destination.txt", "w");
    if (destination == NULL) {
        printf("Error: Cannot create destination file!<br>");
        fclose(source);
        return 1;
    }
    
    /* Copy file character by character */
    while ((ch = fgetc(source)) != EOF) {
        fputc(ch, destination);
    }
    
    printf("File copied successfully!<br>");
    
    /* Close both files */
    fclose(source);
    fclose(destination);
    
    return 0;
}
File copied successfully!

Example: File Copy Using Block Reading

For better performance with large files, we can copy data in blocks using fread() and fwrite() ?

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

#define BUFFER_SIZE 1024

int main() {
    FILE *source, *destination;
    char buffer[BUFFER_SIZE];
    size_t bytes_read;
    
    /* Open files */
    source = fopen("source.txt", "rb");
    if (source == NULL) {
        printf("Error: Cannot open source file!<br>");
        return 1;
    }
    
    destination = fopen("copy_destination.txt", "wb");
    if (destination == NULL) {
        printf("Error: Cannot create destination file!<br>");
        fclose(source);
        return 1;
    }
    
    /* Copy file in blocks */
    while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, source)) > 0) {
        fwrite(buffer, 1, bytes_read, destination);
    }
    
    printf("File copied using block method!<br>");
    
    /* Close files */
    fclose(source);
    fclose(destination);
    
    return 0;
}
File copied using block method!

Key Points

  • Always check if file operations return NULL or fail before proceeding
  • Use "r" mode for reading and "w" mode for writing text files
  • Use "rb" and "wb" modes for binary files
  • Always close files using fclose() to free system resources
  • Block copying is more efficient for large files than character-by-character copying

Conclusion

File copying in C can be accomplished using character-by-character reading with fgetc() and fputc(), or more efficiently using block operations with fread() and fwrite(). Proper error handling and resource cleanup are essential for robust file operations.

Updated on: 2026-03-15T13:11:08+05:30

23K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements