Task 1 20 points Modify myeatc so it will only print out odd

Task 1 (20 points) Modify myeat.c so it will only print out odd lines (1,3, 5,7, Name your source file noteven.c Your program should work like this. hb 117 Quxb41 cat atory.txt Mary has a dog Mary has a cat Tom has a cat Peter has a dog Peter has a parrot Mike has a monkey Jim has a donkey Moline has a dog hbl17euxb4 S gcc Wall noteven.c -o note ven hb 117 auxb4 S ./notevens Mary has a dog Tom has a cat Peter has a parrot Jim has a donkey

Solution

Here is code:

#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{

if(argc!=2){ return -1;
}
char *filename=argv[1];
int input = open(filename,O_RDONLY);
int reset = 0;
if(input<0){
printf(\"Cannot open file %s: %s\ \",filename, strerror(errno));
return -1;
}
char c[1];
while(read(input, c, 1)>0)
{
// checks if c is newline then set reset to 1 to skip next line ..and reset back to 0 again it \ matches
if(*c == \'\ \')
reset = reset == 0 ? 1 : 0;
if(reset == 0)
write(1, c, 1);
}
close(input); return 0;
}

Output:

Mary has a dog
Tom has a cat
Peter has a parrot
Jim has a donkey

 Task 1 (20 points) Modify myeat.c so it will only print out odd lines (1,3, 5,7, Name your source file noteven.c Your program should work like this. hb 117 Qux

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site