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
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>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.