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

Linux Basic I/O please solve task two using C Tasks Task 1\'(50 points) Implemen

ID: 3870075 • Letter: L

Question


Linux Basic I/O please solve task two using C

Tasks Task 1'(50 points) 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 ahou. 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 musto), read), write) and close) system calls. 10 points each. Task 2 (50 points) Modify the above program to iplement a program called body simlar to UNIX commads head or tail. It should priut from the mth line to the nth line of the given file. It should have the followig syutax $body m fileName The above program displays the contents of the fileed fileName, from line n to line u. Each system call must have error checking, and print an eror message if it occurs. You must), read), write), and close ) syste calls. 10 points each

Explanation / Answer

//task1

#include <stdio.h>

#include<fcntl.h>

int main(int argc, char **argv)
{
  
int fd;
char buf[200];
char ch;
int i = 0;
  
if(argc < 2)
{
printf("Please pass file name to read as command line argument ");
return -1;
}
fd = open(argv[1],O_RDONLY,0);
if(fd < 0)
{
printf("Error in reading file ");
return -1;
}
while(read(fd,&ch,1) > 0)
{
buf[i++] = ch;
if(ch == 10)
{
buf[i] = '';
//print line
printf("%s",buf);
i = 0;

}
}
buf[i] = '';
printf("%s",buf);
return 0;
}

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

//output1

Welcome to GDB Online.                                                                                                        

GDB online is an online compiler and debugger tool for C, C++, Python.                                                        

Code, Compile, Run and Debug online from anywhere in world.                                                                   

                                                                                                                              

*******************************************************************************/

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

//task2

#include <stdio.h>
#include<fcntl.h>

int main(int argc, char **argv)
{
  
int fd;
char buf[200];
char ch;
int i = 0,m,n,lineno=0,print=0;
  
if(argc < 4)
{
printf("Please pass mth and nth line and file name to read as command line argument ");
return -1;
}
//now assign the mth line to the argument 1
m = atoi(argv[1]);
//now assign the nth line to the argument 2
n = atoi(argv[2]);
//check m and n are positive numbers
if( m < 0 || n < 0)
{
printf("Please enter positive numbers for line numbers ");
return -1;
}
//open the file passed as third argument
fd = open(argv[3],O_RDONLY,0);
if(fd < 0)
{
printf("Error in reading file ");
return -1;
}
while(read(fd,&ch,1) > 0)
{
buf[i++] = ch;
if(ch == 10)
{
buf[i] = '';
//print line
i = 0;
++lineno;
print= 0;
}
if(!print)
if(lineno >= m && lineno <=n)
{
printf("%s",buf);
print = 1;
}
}
  
buf[i] = '';
if(lineno >= m && lineno <=n)
printf("%s",buf);
return 0;
}

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

//otput2 for command

./a.out 1 4 FileName.txt

//output

Welcome to GDB Online.                                                                                                         

GDB online is an online compiler and debugger tool for C, C++, Python.                                                         

Code, Compile, Run and Debug online from anywhere in world.                                                                    

*******************************************************************************/

Content of text file FileName.txt

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include<fcntl.h>

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