c++ create a cat.cpp. I HAVE BEEN GETTING WRONG ANSWERS FOR THE LONGEST TIME. PL
ID: 3802146 • Letter: C
Question
c++ create a cat.cpp. I HAVE BEEN GETTING WRONG ANSWERS FOR THE LONGEST TIME. PLEASE HELP
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.
c++ create a cat.cpp that does following:
When user types in command cat – filename.txt, it allows user to enter an input and the system will echo back the user input and When ctrl + d is pressed, it exits out of cat mode and displays the contents of the file.
For example:
s <- user input
s <- automatically echoes back user input
dqewfqwef <- user input
dqewfqwef <- automaticcally echoes back user input
c++ rules <- 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.
For example:
c++ rules <- displays contents of filename
c++ is fun <- displays contents of filename1
displays contents of both filename and filename1 and ends the cat method.
Explanation / Answer
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
#include <iostream>
main( int argc,char *argv[3] )
{
for (int j = 1; j < argc; ++j)//read multiple files
{
/* code */
int fd,i;//file descriptor
char buf[2];//store content of file in buf
fd=open(argv[j],O_RDONLY,0777);
if(fd==-argc)
{
printf("file open error");
}
else
{
while((i=read(fd,buf,1))>0)//print content
{
printf("%c",buf[0]);
}
close(fd);
}
}
}
====================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ cat.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out data1.txt data2.txt
We have many
a very good
days vacation next
month ! !
have
a
good
vacation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.