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.

C




// C program to implement the cat view file functionality
#include <stdio.h>
#include <string.h>
  
// function to print file contents
void print_file(const char* filename)
{
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("Unable to open file %s\n", filename);
        return;
    }
  
    // Read and print the file
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
  
    // Close the file
    fclose(file);
}
  
// driver code
int main(int argc, char* argv[])
{
    FILE* file;
    char ch;
  
    // Check if filename was given or not
    if (argc != 2) {
        printf("Usage: %s filename\n", argv[0]);
        return 1;
    }
  
    // calling function to read file
    print_file(argv[1]);
  
    return 0;
}


How to run ?

Now that we have a grasp on reading a file, we can run our program in the command line by following this steps:

gcc cat.c -o cat  //compiles the C program in cat.c and outputs the resulting executable to a file named cat

cat file1.txt //this is how u need to run you command in cmd

Example

Note: Please ensure that file1.txt is present in your current working directory other wise it is going to throw the error.

Viewing the Content of Multiple Files

In the above program, we can only view single file at a time but cat commands lets you view multiple files in a single comment. So let’s progress to the next step: reading content from multiple files simultaneously.

C




// C program to implement multiple file viewing capability
#include <stdio.h>
  
// Function to print the contents of a file
void print_file(const char* filename)
{
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        // Print an error message if the file cannot be
        // opened
        printf("Unable to open file %s\n", filename);
        return;
    }
  
    int ch;
    // Loop through the file and print each character until
    // EOF is reached
    while ((ch = fgetc(file)) != EOF) {
        putchar(ch);
    }
  
    fclose(file);
}
  
int main(int argc, char* argv[])
{
    // Check if the program is called with the correct
    // number of arguments
    if (argc < 2) {
        // Print usage information if no filenames are
        // provided
        printf("Usage: %s filename1 [filename2 ...]\n",
               argv[0]);
        return 1;
    }
  
    // Loop through the provided filenames and print their
    // contents
    for (int i = 1; i < argc; ++i) {
        // Print the current filename
        printf("%s :\n", argv[i]);
        // Call the function to print the contents of the
        // file
        print_file(argv[i]);
        printf("\n");
    }
  
    return 0;
}


How to use?

We can just specify the multiple filenames as different command line arguments to print as many files as we want:

./cat file1 file2 ....

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....