Question: 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;

}




 I wrote a program that would count a specific character in

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!