subject

Using C language, alter the program such that only the correct output is sent to the standard output stream (stdout), while error and help messages are sent to the standard error stream (stderr).

(Hint:use fprintf.)

See the expected output listed in the comment at the top of main. c for an example of what should go to stdout.

#include
#include
#include
#include "smp0_tests. h"

#define LENGTH(s) (sizeof(s) / sizeof(*s))

/* Structures */
typedef struct {
char *word;
int counter;
} WordCountEntry;

int process_stream(WordCountEntry entries[], int entry_count)
{
short line_count = 0;
char buffer[30];

while (gets(buffer)) {
if (*buffer == '.')
break;
/* Compare against each entry */
int i = 0;
while (i < entry_count) {
if (!strcmp(entries[i].word, buffer))
entries[i].counter++;
i++;
}
line_count++;
}
return line_count;
}

void print_result(WordCountEntry entries[], int entry_count)
{
printf("Result:\n");
while (entry_count-- > 0) {
printf("%s:%d\n", entries->word, entries->counter);
}
}

void printHelp(const char *name)
{
printf("usage: %s [-h] ... \n", name);
}

int main(int argc, char **argv)
{
const char *prog_name = *argv;

WordCountEntry entries[5];
int entryCount = 0;

/* Entry point for the testrunner program */
if (argc > 1 && !strcmp(argv[1], "-test")) {
run_smp0_tests(argc - 1, argv + 1);
return EXIT_SUCCESS;
}

while (*argv != NULL) {
if (**argv == '-') {

switch ((*argv)[1]) {
case 'h':
printHelp(prog_name);
default:
printf("%s: Invalid option %s. Use -h for help.\n",
prog_name, *argv);
}
} else {
if (entryCount < LENGTH(entries)) {
entries[entryCount].word = *argv;
entries[entryCount++].counter = 0;
}
}
argv++;
}
if (entryCount == 0) {
printf("%s: Please supply at least one word. Use -h for help.\n",
prog_name);
return EXIT_FAILURE;
}
if (entryCount == 1) {
printf("Looking for a single word\n");
} else {
printf("Looking for %d words\n", entryCount);
}

process_stream(entries, entryCount);
print_result(entries, entryCount);

return EXIT_SUCCESS;
}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 22:50
An environmental protection agency study of 12 automobiles revealed a correlation of 0.47 between engine size and emissions. at 0.01 significance level, can we conclude that there is a positive association between the variables? what is the p value? interpret.
Answers: 2
question
Computers and Technology, 24.06.2019 10:40
Joe needs to see the slide transitions and animations he has applied to his slides in a large view. which presentation view should he use? in which tab would joe find the animations option to make further changes, if any?
Answers: 1
question
Computers and Technology, 24.06.2019 11:00
Need fast im timed in a paragraph of 125 words, explain at least three ways that engineers explore possible solutions in their projects.
Answers: 2
question
Computers and Technology, 24.06.2019 18:30
After making a powerpoint presentation about a new line of clothing designs, henri notices that he used the word ā€œgorgeousā€ on nearly every slide. what would be the  best  way to add more variety to his wording by using tools within powerpoint? using the thesaurus under the view tab, and then using the find dialog box to find and replace every instance of ā€œgorgeousā€using the spelling checker under the view tab, and then using the find dialog box to find every instance of ā€œgorgeousā€ and change some of themusing the thesaurus under the review tab, and then using the find dialog box to find every instance of ā€œgorgeousā€ and change some of themusing the spelling checker under the review tab, and then using the find dialog box to find and replace every instance of ā€œgorgeousā€
Answers: 2
You know the right answer?
Using C language, alter the program such that only the correct output is sent to the standard output...
Questions
question
English, 04.02.2021 21:40
question
Mathematics, 04.02.2021 21:50
question
Mathematics, 04.02.2021 21:50
question
History, 04.02.2021 21:50
question
Mathematics, 04.02.2021 21:50
question
Mathematics, 04.02.2021 21:50
Questions on the website: 13722362