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

Code given below for Task 1, please modify it for task 2 and answer task 2. Than

ID: 3891902 • Letter: C

Question

Code given below for Task 1, please modify it for task 2 and answer task 2. Thanks and will rate :)
Linux, C programming.

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<string.h>

main( int argc, char **argv )

{
int fd,i;
char buf[2];
char buf_error_message[20] = "file open error";
fd=open(argv[1],O_RDONLY,0777);
if(fd < 1)
{

//write error message

if( write(1, buf_error_message, strlen(buf_error_message)) < 0 ) {

perror("Error while write system call");

}

}

else

{

while((i=read(fd,buf,1))>0)

{

// display contents

if( write(1, buf, 1) < 0){

perror("Error while write system call");

}

}

if(close(fd) < 0){

perror("Error while close system call");

}

}

Task Implement a program to display a file's contents. It should be similar to the UNIX cat command when given a single file. Name your command show. It should have the following syntax: Show fileName The above program displays the contents of the file named fileName. Each system call must have error checking, and print an error message if it occurs. You must use open , read), write and close) system calls.

Explanation / Answer

#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<string.h>

main( int argc, char **argv )

{
int fd,i;
//declare two variable m and n to hold range of lines
int m,n,line = 0,first=0;
char buf[2];
char buf_error_message[20] = "file open error";

if(argc < 4)
{
printf("Insufficient arguments,input should be similar to ./program name m n filename where m and n are integres representing from and till line numbers ");
return -1;
}
fd=open(argv[3],O_RDONLY,0777);
m=atoi(argv[1]);
n=atoi(argv[2]);
if( m < 0 || n < 0)
{
printf("m or n cannot be less than zero ");
return -1;
}
if(fd < 1)
{

//write error message

if( write(1, buf_error_message, strlen(buf_error_message)) < 0 ) {

perror("Error while write system call");

}

}

else

{

while((i=read(fd,buf,1))>0)

{

// display contents
if(buf[0] ==' ' )
++line;
if(line >= m-1 && line < n)
{
if(!first && buf[0] == ' ')
continue;
first=1;
if( write(1, buf, 1) < 0)
{
  
perror("Error while write system call");
  
}
}
  
}

if(close(fd) < 0){

perror("Error while close system call");

}

}

}

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

//output

//invalid input given

./programname -1 5 file.txt

m or n cannot be less than zero

./program file.txt

Insufficient arguments,input should be similar to ./program name m n filename where m and n are integres representing from and till line numbers

//correct output

./programname 2 5 file.txt

The term C-string refers to the classic implementation of strings in the C programming language.                                    

A C-string is a sequence of characters terminated by the null character, ''.                                                      

Since C is part of C++, C-string implementations are valid in C++ as well as C.                                                     

char myString[100];

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