✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
A short note on function fgets
Function fgets reads lines from an input stream. The function takes 3 parameters:
So the call:
fgets(buf, 81, infile);says to read up to characters from input file stream infile and store these characters into an array whose address is specified by buf. The function will read less than characters if it reaches the end of the file or if its reads a newline character first. In any case, fgets stores a null character immediately after the last character written to buf.
Since fgets takes an upper bound on the number of characters to read, it should be the function of choice especially if you're not sure how long the lines you're reading are. For example, fgets(buf, 81, stdin) will read up to characters from standard input stream even if there are more characters in the current line.
Finally, fgets will return its first parameter on success; otherwise the function will return NULL when it reaches the end of the file without reading any characters.
A short note on function fputs
Function fputs writes a line to a specified stream. That is, the call
fputs(buf, outfile);writes the contents of array buf to output file stream outfile. Note that fputs does not append a newline character to the stream. Therefore, the statement
fputs(buf, stdout);is equivalent to the statement
printf("%s", buf);Question
Consider the execution of the following code fragment:
#define MAX_LEN (257)char str[MAX_LEN];
fputs("Enter a sentence: ", stdout);
fgets(str, MAX_LEN, stdin);
fputs(str, stdout);
Suppose in response to the program's prompt, a user enters the text today is a good day through the keyboard [followed by the key]. Now, write the exact text written to the standard output stream by the program's final statement including any newline chars.