💻 Unit 5 – Part C (15-Mark Q&A)

Programming in C

⬅ Back to Unit 5

Ad Space

Part C: 15-Mark Question

Write a C program to copy the contents from one file to another file and to merge two files.

This program demonstrates both file copying and file merging. We will handle each part sequentially in the main() function for clarity. For this example, we will also create the initial 'source.txt', 'file1.txt', and 'file2.txt' so the program is fully runnable and can be tested immediately.

Part 1: Copying a File (source.txt to destination.txt)

This part opens a source file in read mode ("r") and a destination file in write mode ("w"). It reads characters one by one from the source and writes them to the destination until the end-of-file (EOF) is reached.

Part 2: Merging Two Files (file1.txt and file2.txt into merged.txt)

This part opens two source files in read mode ("r") and a target file in write mode ("w"). It first copies the entire content of file1.txt to merged.txt, and then appends the entire content of file2.txt to merged.txt.

Full C Program:

#include <stdio.h>
#include <stdlib.h> // For exit()

int main() {
    FILE *sourceFile, *destFile;
    FILE *file1, *file2, *mergedFile;
    char ch;

    // --- Part 1: Copy File ---
    printf("--- Part 1: Copying File ---\n");
    
    // Create a dummy source file for the example
    sourceFile = fopen("source.txt", "w");
    if(sourceFile == NULL) {
        printf("Error creating source.txt!\n");
        exit(1);
    }
    fprintf(sourceFile, "This is the source file.\nIt has multiple lines.\nEnd of source.\n");
    fclose(sourceFile);

    // Open source file for reading
    sourceFile = fopen("source.txt", "r");
    if (sourceFile == NULL) {
        printf("Error opening source.txt!\n");
        exit(1);
    }

    // Open destination file for writing
    destFile = fopen("destination.txt", "w");
    if (destFile == NULL) {
        printf("Error opening destination.txt!\n");
        fclose(sourceFile);
        exit(1);
    }

    // Copy character by character
    while ((ch = fgetc(sourceFile)) != EOF) {
        fputc(ch, destFile);
    }

    printf("File copied successfully to destination.txt.\n\n");

    // Close the files
    fclose(sourceFile);
    fclose(destFile);

    // --- Part 2: Merge Files ---
    printf("--- Part 2: Merging Files ---\n");

    // Create dummy files for merging
    file1 = fopen("file1.txt", "w");
    if(file1) {
        fprintf(file1, "This is the first file.\nContent from file 1.\n");
        fclose(file1);
    }
    
    file2 = fopen("file2.txt", "w");
    if(file2) {
        fprintf(file2, "This is the second file.\nContent from file 2.\n");
        fclose(file2);
    }

    // Open files for merging
    file1 = fopen("file1.txt", "r");
    if (file1 == NULL) {
        printf("Error opening file1.txt!\n");
        exit(1);
    }

    file2 = fopen("file2.txt", "r");
    if (file2 == NULL) {
        printf("Error opening file2.txt!\n");
        fclose(file1);
        exit(1);
    }

    // Open merged file for writing
    mergedFile = fopen("merged.txt", "w");
    if (mergedFile == NULL) {
        printf("Error opening merged.txt!\n");
        fclose(file1);
        fclose(file2);
        exit(1);
    }

    // Copy contents of first file to merged.txt
    while ((ch = fgetc(file1)) != EOF) {
        fputc(ch, mergedFile);
    }

    // Copy contents of second file to merged.txt
    while ((ch = fgetc(file2)) != EOF) {
        fputc(ch, mergedFile);
    }

    printf("Files merged successfully into merged.txt.\n");

    // Close all files
    fclose(file1);
    fclose(file2);
    fclose(mergedFile);

    return 0;
}

Explanation:

  1. Includes: <stdio.h> is included for all file operations (fopen, fgetc, fputc, fclose, fprintf) and <stdlib.h> is included for the exit() function, which is used to terminate the program if a file error occurs.
  2. File Pointers: Variables of type FILE* are declared to store the references to the files being handled.
  3. Error Handling: After every fopen() call, the program checks if the returned file pointer is NULL. A NULL value indicates that the file could not be opened (e.g., file not found, no permissions). If an error occurs, a message is printed, and exit(1) is called to stop the program.
  4. File Modes:
    • "w" (write): Used for creating the initial dummy files and for the destination files (destination.txt, merged.txt). This mode creates a new file or overwrites an existing file.
    • "r" (read): Used for all source files (source.txt, file1.txt, file2.txt) to read their contents.
  5. Copy/Merge Logic: The core logic relies on a while loop: while ((ch = fgetc(sourceFile)) != EOF).
    • fgetc(sourceFile) reads a single character from the source file.
    • The character is assigned to ch.
    • This value is compared to EOF (End Of File).
    • If it's not the end of the file, fputc(ch, destFile) writes the character to the destination file.
    • This loop continues until the entire source file is read.
  6. Closing Files: fclose() is called for every file that was successfully opened. This is crucial to save changes and free up system resources.

Ad Space