![]() |
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 ProgramThis program will implement 3 functionalities of the ‘cat’ command. There are:
1. View the Content of the FileTo 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
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 Example![]()
Viewing the Content of Multiple FilesIn 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
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 FileNow, 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
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 FilesThe 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
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![]() ConclusionCongratulations 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! |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |