Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I wrote a program that would count a specific character in a file u and d's The program must output to stdout if the number

I wrote a program that would count a specific character in a file u and d's

The program must output to stdout if the number read is 'u' and 'd' but output to stderr the number of characters read that are not 'u' or 'd'. And the program must return 0 to the calling shell if at least one 'u' or 'd' was found in the collection of files specified on the command line. Otherwise, the program must return 1 the calling shell. However I am having trouble checking if the output for stderr and also the return call.

Here is my code and output of what I have for reference



#include

#include

#include

#include

#include


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

int UDcount = 0;

int otherCount = 0;


for(int i = 1; i

const char *path = argv[i];

int fd = open(path, O_RDONLY);

if(0>fd){

printf("opening to: %d ",errno);

}else{

char buffer[1] = {0};

int status = 0;

status = read(fd,buffer,1);

do{

if(*buffer == 'U' | *buffer == 'u' | *buffer == 'D' | *buffer == 'd'){

UDcount++;

}

else{

otherCount++;

}

status = read(fd,buffer,1);

}while(0

}

close(fd);

}

fprintf(stdout," the UD count: %d", UDcount);

fprintf(stderr," the not UD count: %d", otherCount);

return 0;

}




image text in transcribed
osboxes oreuntib countrestrtxt b.txt che noe vo cornet ohe vo sovet el osboxes

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

Students also viewed these Databases questions