Concatenate Two Files

The cat commands also provide the functionality to concatenate the content of one file to another file. We can also implement this feature in our program.

C




// Final C program to implement the functionality of the cat
// command
#include <stdio.h>
#include <string.h>
  
// Function to print the contents of a file
void print_file(const char* filename)
{
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Unable to open file %s\n", filename);
        return;
    }
  
    int ch;
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
  
    fclose(file);
}
  
// Function to write input from the user to a file
void write_to_file(const char* filename)
{
    FILE* file = fopen(filename, "w");
    if (file == NULL) {
        printf("Unable to open file %s\n", filename);
        return;
    }
  
    char buffer[512];
    fgets(buffer, 512, stdin);
    fputs(buffer, file);
  
    fclose(file);
}
  
// Function to concatenate the contents of two files
void concatenate_files(const char* filename1,
                       const char* filename2)
{
    FILE* file1 = fopen(filename1, "r+");
    if (file1 == NULL) {
        printf("Unable to open file %s\n", filename1);
        return;
    }
  
    FILE* file2 = fopen(filename2, "r");
    if (file2 == NULL) {
        printf("Unable to open file %s\n", filename2);
        fclose(file1);
        return;
    }
  
    // Move file pointer to the end of the first file
    fseek(file1, 0, SEEK_END);
  
    int ch;
    while ((ch = fgetc(file2)) != EOF) {
        fputc(ch, file1);
    }
  
    fclose(file1);
    fclose(file2);
}
  
// driver code
int main(int argc, char* argv[])
{
    if (argc < 2) {
        // Print usage information if no filenames are
        // provided
        printf("Usage: %s filename1 [filename2 ...]\n",
               argv[0]);
        return 1;
    }
  
    for (int i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "-") == 0) {
            // If the argument is "-", write to the
            // specified file
            write_to_file(argv[++i]);
        }
        else if (i + 1 < argc
                 && strcmp(argv[i + 1], "-") == 0) {
            // If the next argument is "-", concatenate the
            // two specified files
            concatenate_files(argv[i], argv[i + 2]);
            i += 2;
        }
        else {
            // Print the contents of the file
            printf("%s :\n", argv[i]);
            print_file(argv[i]);
            printf("\n");
        }
    }
    return 0;
}


How to Use?

The syntax to use the concatenation functionality is similar to the cat command:

./cat dest_file - source_file

The content of the source_file will be appended to the end of the dest_file.

Example

Build Your Own ‘cat’ Command in C for Linux

You may have heard about cat command which is a Linux command. It stands for concatenate and plays an important role in Unix-like operating systems by helping to concatenate and display file contents. Despite the simple name, cat does a lot of work and goes beyond just putting those files together. This allows users to easily create, view, and merge multiple files, making data management much easier.

Additionally, its ability to redirect output to other commands or files gives it great importance in shell scripting and command-line tasks.

In this article, we’ll explore the inner workings of the ‘cat’ command by creating our version with C which is capable of viewing the content of the file, writing the content to the file, and also concatenating two files.

Similar Reads

Features of C ‘cat’ Command Program

This program will implement 3 functionalities of the ‘cat’ command. There are:...

1. View the Content of the File

To begin, we’ll focus on implementing the functionality to view the contents of a single file. So it will be easy to implement viewing multiple files further....

2. Write the Content of a File

...

3. Concatenate Two Files

...

Conclusion

Now, let’s move on to implementing the ability to write content to a file. This step will empower us to not only read but also modify and save information within a file....