Copying the contents of one file to another in C involves reading data from the source file and writing it to the destination file. This process is useful for file management tasks, such as creating backups or duplicating data.

Program: Copy Contents of One File to Another in C

Here’s a simple program that demonstrates how to copy the contents from one file to another.

#include <stdio.h> #include <stdlib.h> int main() { FILE *sourceFile, *destFile; char ch; // Open the source file in read mode sourceFile = fopen("source.txt", "r"); if (sourceFile == NULL) { perror("Error opening source file"); return EXIT_FAILURE; } // Open the destination file in write mode destFile = fopen("destination.txt", "w"); if (destFile == NULL) { perror("Error opening destination file"); fclose(sourceFile); // Close the source file before exiting return EXIT_FAILURE; } // Copy contents from source file to destination file while ((ch = fgetc(sourceFile)) != EOF) { fputc(ch, destFile); // Write the character to the destination file } printf("Contents copied successfully from source.txt to destination.txt\n"); // Close both files fclose(sourceFile); fclose(destFile); return EXIT_SUCCESS; }

Explanation of the Program

  1. Header Files:

    • #include <stdio.h>: Includes standard I/O functions for file handling.
    • #include <stdlib.h>: Provides definitions for exit status and error handling.
  2. File Pointers:

    • FILE *sourceFile, *destFile;: Declares pointers to handle the source and destination files.
  3. Open Source File:

    • Opening:
      sourceFile = fopen("source.txt", "r");
      This line attempts to open source.txt in read mode.
    • Error Checking:
      if (sourceFile == NULL) { perror("Error opening source file"); return EXIT_FAILURE; }
      If the file can't be opened (e.g., it doesn't exist), an error message is displayed and the program exits.
  4. Open Destination File:

    • Opening:
      destFile = fopen("destination.txt", "w");
      This opens (or creates) destination.txt in write mode.
    • Error Checking: Similar to the source file, it checks for errors when opening the destination file.
  5. Copying Process:

    • The program uses a loop to read characters from the source file one at a time:
      while ((ch = fgetc(sourceFile)) != EOF) { fputc(ch, destFile); }
      • fgetc(sourceFile) reads a single character from sourceFile.
      • The loop continues until the end of the file (EOF) is reached.
      • fputc(ch, destFile) writes the read character to destFile.
  6. Closing Files:

    • After copying is complete, both files are closed:
      fclose(sourceFile); fclose(destFile);
    • This ensures that all resources are properly released.
  7. Program Output:

    • After the successful completion of the file copy operation, a message is printed:
      printf("Contents copied successfully from source.txt to destination.txt\n");

How to Run the Program

  1. Create the Source File: First, create a text file named source.txt in the same directory as the program and add some text to it.

  2. Compile the Code: Use a C compiler like gcc to compile the code:

    gcc copy_file.c -o copy_file
  3. Execute the Program: Run the compiled program:

    ./copy_file

Example Output

Assuming source.txt contains the following text:

Hello, World! This is a test file.

After running the program, the output will be:

Contents copied successfully from source.txt to destination.txt

The contents of destination.txt will now be:

Hello, World! This is a test file.

Conclusion

This program illustrates the basic file handling operations in C: opening files, reading from a source file, writing to a destination file, and closing the files afterward. Understanding how to work with files is a foundational skill in C programming, enabling you to manage data effectively and efficiently. This method can be expanded for more complex file operations, such as error logging, binary file copying, or even merging multiple files.