Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ (create cat.cpp) implement your own version of the cat systems program, a st

ID: 3801623 • Letter: C

Question

c++ (create cat.cpp) implement your own version of the cat systems program, a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files.

When user types in command cat – filename.txt, it allows user to enter an input and the system will echo back the user input. When ctrl + d is pressed, it exits out of cat mode and displays the contents of the file.

When user types in command cat filename.txt file1.txt, it will concatenate the contents of file and file1 and display the contents of both files.

You are NOT allowed to use the exec, system, popen, and pclose system calls or functions in your implementation. You are not allowed to call the existing cat implementation. You must supply your own implementation that uses low-level I/O. Low-level file I/O is a requirement.

Explanation / Answer

#include<iostream>
#include <fstream>
#include<stdio.h>
#include <signal.h>
using namespace std;


void DisplayFile(string file )
{
ifstream fd(file);
string line ;

if (fd.is_open())
{
   while(getchar()!=EOF); //loop untill one press ctrl+d

    while ( getline (fd,line) )
    {
      cout << line << ' ';
    }
    fd.close();
}

else
   {
     cout<<"Error in file opening";
     exit(0);
   }
}

void concateFile(string file1,string file2)
{
ofstream fd1(file1, ios::app);
ifstream fd2(file2);
string line ;


if(fd1.is_open() && fd2.is_open())
{
  
     while ( !fd2.eof())
    {
      getline (fd2,line);
      fd1<< line << ' ';
    }
    fd1.close();

    ifstream fd1(file1, ios::in);

    while(getchar()!=EOF);

    while ( getline (fd1,line) )
    {
      cout<< line << ' ';
    }
    fd1.close();
  
}
else
{
    cout<<"Error in opening files ";
    exit(0);
}
}


int main(int argc,char** argv)
{

string filename1,filename2;


   if(argc==1)
   {
     cout<<"Too few arguments";
     exit(0);
   }
   else
       if(argc>3)
       {
         cout<<"Too many arguments";
         exit(0);
       }

if(argc==2)
{
    filename1.assign(argv[1]);
}
else
{
    filename1.assign(argv[1]);
    filename2.assign(argv[2]);
}

int choice=argc-1;

switch(choice)
{
   case 1:
           DisplayFile(filename1);
           break;
   case 2:
           concateFile(filename1,filename2);
           break;
   default:
           break;
}

return 0;
}
     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote