USE C LANGUAGE ONLY For this assignment you will rewrite the

USE C LANGUAGE ONLY.

For this assignment, you will rewrite the cat program from HW01. This time, however, all interactions with the file will be used with system calls: open, read, close, write.

Additionally, use errno to report how the file failed to open, if it does fail to open. Check for error caused by no such file existing, insufficient permissions, and input/output error ( use man pages for more info ). Display an appropriate error message for each.

Hints:

You can use write to display stuff to the screen. File descriptor 1 will cause bytes written by write to be sent to the screen.

A 10-Point Sample Run:

Sample Run belowwwwwwwww in picture :

--------------------------------------------------

Code I USED IN ASSIGNMENT ONE. JUST HAVE REWRITE THIS

#include \"stdio.h\"

#include \"stdlib.h\"

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

if(argc > 1){

FILE* fp;

char *line = NULL;

int len = 300;

int i;

  

for(i = 1; i < argc; ++i)

{

fp = fopen(argv[i], \"r\");

if(fp == NULL){

printf(\"cat: %s: No such file or directory\ \", argv[i]);

}

else{

line = (char *)malloc(len);

while (fgets(line, len, fp)) {

printf(\"%s\ \", line);

}

  

fclose(fp);

  

}

}

}

  

  

  

return 0;

}

Solution

Hi, Please find my code.

Please let me know in case of any issue.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

#define BUF_SIZE 8192

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

int fp; /* Input and output file descriptors */
ssize_t ret_in, ret_out; /* Number of bytes returned by read() and write() */

if(argc > 1){
  
char *line = NULL;
int len = 300;
int i;
  
for(i = 1; i < argc; ++i)
{
fp = open(argv[i], O_RDONLY);
if(fp == -1){
perror(\"No such file or directory\ \");
}
else{
/* Copy process */
while((ret_in = read(fp, &line, len)) > 0){
ret_out = write(STDOUT_FILENO, &line, (ssize_t)ret_in);
if(ret_out != ret_in){
perror(\"write\");
return 4;
}
}
  
}

  
}
}

close (fp);
  

return (EXIT_SUCCESS);
}

USE C LANGUAGE ONLY. For this assignment, you will rewrite the cat program from HW01. This time, however, all interactions with the file will be used with syste
USE C LANGUAGE ONLY. For this assignment, you will rewrite the cat program from HW01. This time, however, all interactions with the file will be used with syste
USE C LANGUAGE ONLY. For this assignment, you will rewrite the cat program from HW01. This time, however, all interactions with the file will be used with syste

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site