Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 32

Merge two files into a third file

CFilesJNTUH R22 · CS108ES

Aim

To write a C program that merges two text files into a third file: the contents of the first file followed by the contents of the second. The three file names are given as command-line arguments.

Algorithm

  1. Check that three file names were given. If not, print how to use the program and stop.
  2. Open the first and second files in read mode ("r") and the third file in write mode ("w"). If any file cannot be opened, close the ones already open, print an error and stop.
  3. Copy the first file to the third one character at a time with fgetc and fputc, until EOF.
  4. Copy the second file to the third in the same way.
  5. Close all three files and print a message.

Program

C
#include <stdio.h>

/* Copy every character of src to the end of what is already in dest */
void copy_file(FILE *src, FILE *dest) {
    int ch;
    while ((ch = fgetc(src)) != EOF) {
        fputc(ch, dest);
    }
}

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Usage: %s <first file> <second file> <merged file>\n", argv[0]);
        return 1;
    }

    FILE *f1 = fopen(argv[1], "r");
    if (f1 == NULL) {
        printf("Cannot open file %s\n", argv[1]);
        return 1;
    }
    FILE *f2 = fopen(argv[2], "r");
    if (f2 == NULL) {
        printf("Cannot open file %s\n", argv[2]);
        fclose(f1);
        return 1;
    }
    FILE *f3 = fopen(argv[3], "w");
    if (f3 == NULL) {
        printf("Cannot create file %s\n", argv[3]);
        fclose(f1);
        fclose(f2);
        return 1;
    }

    copy_file(f1, f3);
    copy_file(f2, f3);

    fclose(f1);
    fclose(f2);
    fclose(f3);
    printf("%s and %s merged into %s\n", argv[1], argv[2], argv[3]);
    return 0;
}

Output

Merging two files

Input file: part1.txt (Text)
Unit 1: Introduction to C
Unit 2: Arrays and strings
Input file: part2.txt (Text)
Unit 3: Pointers and structures
Unit 4: Functions and files
Output 1
$ ./a.out part1.txt part2.txt merged.txt
part1.txt and part2.txt merged into merged.txt
$ cat merged.txt
Unit 1: Introduction to C
Unit 2: Arrays and strings
Unit 3: Pointers and structures
Unit 4: Functions and files

Second file missing

Input file: part1.txt (Text)
Unit 1: Introduction to C
Output 2
$ ./a.out part1.txt part9.txt merged.txt
Cannot open file part9.txt

Recorded by compiling and running this exact program.

How it works

The third file is opened once with "w" and kept open while both files are copied. Each fputc writes after the last character written, so the second file's text comes right after the first file's.

If the first file does not end with a newline, its last line and the second file's first line join into one line in the merged file. The program copies the files exactly and does not add anything between them.

The same copy_file function is used for both input files, so the copying loop is written only once.

Common mistakes

  • Opening the third file with "w" again before copying the second file. That erases the first file's contents. Open it once, or use "a" (append) for the second copy.

  • Not closing the files already opened when a later fopen fails. Close every file that was successfully opened before returning.

  • Forgetting fclose(f3). Writes to a file are buffered, so the last part of the merged text may never reach the file.

  • Giving the merged file the same name as one of the input files. Opening it with "w" empties it before it is read, and its data is lost.

Viva questions

Try to answer each one aloud before you open it.

  • What is the difference between the "w" and "a" modes?

    "w" creates the file or empties it if it exists, and writes from the start. "a" keeps the existing contents and adds new data at the end.

  • Why is copy_file given FILE pointers instead of file names?

    The files are already open, and the third file must stay open so that the second copy continues where the first ended. Passing the FILE * lets the function use the open files directly.

  • What is the value of argc when the program is run as ./a.out a.txt b.txt c.txt?

    4: the program name and three file names.

  • How many files can a program have open at the same time?

    At least FOPEN_MAX files, a constant in stdio.h that is at least 8. Most systems allow many more.

  • How would you merge the files without a third file, adding the second file to the end of the first?

    Open the first file in "a" (append) mode and copy the second file into it.

Learn the concepts

All Programming for Problem Solving Lab experiments →