Build Your Own 'cat' Command in C for Linux
Last Updated :
12 Jan, 2024
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.
Prerequisite: File I/O in C, Command-Line Argument in C
Features of C 'cat' Command Program
This program will implement 3 functionalities of the 'cat' command. There are:
- Viewing the contents of the file.
- Writing the content to a file.
- Concatenating two files.
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.
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
2. Write the Content of a File
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.
C
#include <stdio.h>
#include <string.h>
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);
}
void write_to_file(const char* filename)
{
FILE* file = fopen(filename, "w");
if (file == NULL) {
printf("Unable to open file %s\n", filename);
return;
}
int ch; // or char ch;
while ((ch = getchar()) != EOF) {
fputc(ch, file);
}
fclose(file);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
printf("Usage: %s filename1 [filename2 ...]\n",
argv[0]);
return 1;
}
for (int i = 1; i < argc; ++i) {
// check wather the file is to be written to or read
if (strcmp(argv[i], "-") == 0) {
write_to_file(argv[++i]);
}
else {
printf("%s :\n", argv[i]);
print_file(argv[i]);
printf("\n");
}
}
return 0;
}
How to Use?
We can use this feature as shown below:
./cat - file
You may think about why we are using `-` operator instead of `>`. The reason id that when you run cat > file1.txt, the shell interprets this as "run the cat command and redirect its output to file1.txt". However, in your program, you're treating > as a filename, which is why you're seeing unexpected behavior.Now this program is capable of writing the content on a file we can check this by running `cat - file1.txt`.
The above code not only enables to write the user to file, but is also able to create a new file if the file does not exists.
Example
3. 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
Conclusion
Congratulations on getting your own ‘cat’ command in C! Now you have the skills for working with command-line arguments and file I/O. Attempt to explore new features, such as updating existing files and other feature as well, which will provide more knowledge of file and command-line arguments handling.
Happy coding!
Similar Reads
How to Write a Command Line Program in C?
In C, we can provide arguments to a program while running it from the command line interface. These arguments are called command-line arguments. In this article, we will learn how to write a command line program in C. How to Write a Command Line Program in C? Command line arguments are passed to the
2 min read
Mimic the Linux adduser command in C
Programming for fun can manifest itself in the works of leisurely coding practice as well. Regardless of being academically inadequate, the task certainly does contribute to the programmer's understanding of the resourcefulness of the language. Here let's look at a distinct programming activity that
9 min read
C Program To Print Your Own Name
Printing your own name means displaying your name on the computer screen. In this article, we will learn how to print your own name using a C program. Examples Input: name = "Rahul"Output: RahulExplanation: The program prints "Rahul" to the screen. Input: name = "Vikas"Output: VikasExplanation: The
4 min read
How to Create a Dynamic Library in C?
In C, a dynamic library is a library that is loaded at runtime rather than compile time. We can create our own dynamic libraries and use them in our programs. In this article, we will learn how to create a dynamic library in C. Example: Input:Hello, geeksforgeek!Output:Hello, geeksforgeek!Creating a
2 min read
How to Append a Character to a String in C?
In this article, we will learn how to append a character to a string using the C program. The most straightforward method to append a character to a string is by using a loop traverse to the end of the string and append the new character manually. [GFGTABS] C #include <stdio.h> void addChar(ch
3 min read
CLI programs in C for playing media and shut down the system
Command Line Interface: CLI is a text-based user interface (UI) used to view and manage computer files.Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.In programming, the user gives input during the execution of a progr
6 min read
How to use make utility to build C projects?`
When we build projects in C/C++, we have dependencies among files. For example, there might be a file a.c that calls a function from b.c. So we must compile b.c before a.c. There might be many dependencies in a project and manually following these dependencies and compiling files one by one becomes
5 min read
C/C++ program to print Hello World without using main() and semicolon
The task is to write a program to print Hello World without using main() and semicolon. As we already know, how to print Hello World without the use of a semicolon. Now, for writing without main() method, we will need a Macro. C/C++ Code // C program to print Hello World // without using main() and
1 min read
How to Read a File Line by Line in C?
In C, reading a file line by line is a process that involves opening the file, reading its contents line by line until the end of the file is reached, processing each line as needed, and then closing the file. In this article, we will learn how to read a file line by line in C. Reading a File Line b
2 min read
Create Directory or Folder with C/C++ Program
Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty director
2 min read