Question

In: Computer Science

We are interested in the implementation of a Unix/Linux system utility for the concatenation of a list of n text files.

Write in C

We are interested in the implementation of a Unix/Linux system utility for the concatenation of a list of n text files. In order to do that we are going to consider the following syntax below where the concatenation of n text files are written in the output file all.txt or on the console if the output text file is not specified.

      $./mycat file_1.txt file_2.txt   . . .   file_n.txt > all.txt

Solutions

Expert Solution

C code for above problem

#include
#include

int main(int argc,char **argv)
{
   int found=0;
   for(int i=1;i<argc;i++)
   {
       if(strcmp(argv[i],">")==0 && i==argc-2)
       {
           found=1;
           break;
       }
   }
  
   FILE *fout=NULL;
   if(found)
   {
       fout=fopen(argv[argc-1],"w");
       argc-=2;
   }
   for(int i=1;i<argc;i++)
   {
       FILE *fp=fopen(argv[i],"r");
       if(fp)
       {
           char line[100];
           while(fgets(line,sizeof(line),fp))
           {
               if(found)
               {
                   fprintf(fout,"%s",line);
               }
               else
               {
                   printf("%s",line);
               }
           }
       }
       fclose(fp);
   }
  
   if(found)
   {
       fclose(fout);
   }
   return 0;
}

Sample output

if "file_1.txt" has following data

My data of file-1

"file_2.txt" has following data

My data of file-2

(1):

If our command while running is "./a.out file_1.txt file_2.txt", then output looks as follows: (It means printing data of all files on console)

(2):

If our command while running is "./a.out file_1.txt file_2.txt > all.txt", then "all.txt" looks as follows: (It means writing all files into one file)

My data of file-1
My data of file-2

 


Related Solutions

Pertaining to the Linux operating system, which is a variant of Unix, DISCUSS Topics that will...
Pertaining to the Linux operating system, which is a variant of Unix, DISCUSS Topics that will include things such as history, Linux kernel, and design principles.
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of...
Linux Directories, File Properties, and the File System in C Understanding Unix/Linux Programming Your version of mv command The mv command is more than just a wrapper around the rename system call. Write a version of mv that accepts two argument. The first argument must be the name of a file, and the second argument may be the name of a file or the name of a directory. If the destination is the name of a directory, then mv moves...
Unix/Linux Turn in the following commands and any output from the commands. 1) ll to show the original 3 files
Unix/LinuxTurn in the following commands and any output from the commands.1) ll to show the original 3 files2) run the tar command to stuff three files3) ll to show the 'tar archive'4) mkdir newdir to create a new directory to unstuff the 'tar archive'
System Administration Linux/Unix This lab is for you to research and implement old school DNS for...
System Administration Linux/Unix This lab is for you to research and implement old school DNS for your virtualized environment Currently, if you tried to ping your server01 by IP address (192.168.10.11), you would receive a response. If you tried to ping using its hostname "server01", it would result in "ping: hostname: Temporary failure in name resolution" 1) Research how and where to implement 2) Create the following entries: 192.168.10.1 router 192.168.10.11 SRV1 192.168.10.12 SRV2 192.168.10.13 client 3) Ensure these hostnames...
Unix ramdom question. How do you display a list of files in your current directory, sorted...
Unix ramdom question. How do you display a list of files in your current directory, sorted by file size with the largest files at the top.
1. Discuss the various log files found on a Linux system. Based on their own experiences,...
1. Discuss the various log files found on a Linux system. Based on their own experiences, how often do they feel they would check these files, and how vigilant do they think they would be about the process? Have them outline some of the reasons why an administrator might tend to ignore these files. 2. Discuss there experiences with the creation of user and group accounts in other OSs such as Windows 10 or Windows Server 2019. What do they...
assume a program in unix that have a multiple system calls how to show the list...
assume a program in unix that have a multiple system calls how to show the list of system calls used in the program?
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
Suppose we have a substring of length m and text of size n. Write an algorithm...
Suppose we have a substring of length m and text of size n. Write an algorithm to find out if the substring is present in the text or not. What is the complexity of your algorithm in terms of m and n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT