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;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started