A program successfully opens separate input and output file streams. Its input loop stops when fgets returns NULL; immediately afterward, ferror(in) is nonzero and feof(in) is zero. Separately, fprintf(out, "%s\n", "ready") returns 6, but the subsequent fclose(out) returns EOF. Which conclusions are supported? Select every correct statement.
The null input result establishes that normal end-of-file was reached.
The input loop stopped with a read error reported on its stream.
The positive output result guarantees all six characters reached the file.
The output must be reported as unsuccessful because closing reported failure.
Unlock full course notes by signing up
Tracing characters and terminators
A Unix text file contains exactly ABC\nDEFGHIJ\nK, where each \n denotes one newline and every letter occupies one byte. After successfully opening the file for reading, a program repeatedly calls fgets(buf, sizeof buf, in) with char buf[7]. No read errors occur. For each of the first four calls, enter the number of input characters stored and the array bytes occupied by those characters plus their terminating null character. Count newlines as input characters and give exact integers; ignore unused array positions.
Completing a safe input loop
A text-processing function has already checked that in is a valid readable stream and has allocated a sufficiently large character array. Complete its procedure using exact C identifiers: read each chunk with ________, process the array only when the returned pointer differs from ________, and after reading stops test ________ to determine whether the stream reports a read error.
Delivering a prompt before reading
A command-line C program successfully configures stdout as fully buffered before performing any input or output. It then successfully executes:
The program will next call fgetc(stdin). It must explicitly deliver any pending prompt characters to the surrounding system before attempting input, without closing stdout or changing its buffering policy.
Complete the following policy using a standard C function name without parentheses, a stream identifier, and a decimal integer return value.
Counting reads through shared stream pointers
A C diagnostic program reads an unchanged file containing exactly 40 bytes. Both calls to fopen below succeed. No input errors occur, and both calls to ungetc succeed.
The helper drain(s) repeatedly calls fgetc(s) until it returns EOF. The helper returns the number of calls that returned characters, excluding the final call that returned EOF. It neither closes nor repositions the stream.
All 24 calls to fgetc in the three initial loops return characters. Find first, second, and third, each measured in characters returned. Also find the total number of characters returned by fgetc across the initial loops and all three drains. Count repeated delivery of a character each time it occurs, and exclude the return values of ungetc. Give all four answers as exact integers.