C standard I/O library: what does rewind(stream) do to a file stream? Choose the most accurate behavior per ISO C for a valid open stream.

Difficulty: Easy

Correct Answer: Reposition the file pointer to beginning of file.

Explanation:


Introduction / Context:
The function rewind(stream) is part of the C standard I/O library. This question tests understanding of file positioning and status flags for streams opened via fopen or related functions.


Given Data / Assumptions:

  • stream is a valid FILE* opened earlier.
  • We are using ISO C semantics for rewind.
  • Error and end-of-file indicators may have been set previously.


Concept / Approach:

rewind(stream) moves the file position indicator to the beginning of the file and clears both the error and end-of-file indicators for that stream. It does not return a value. This contrasts with functions like fseek/fseeko which return status codes and can position relative to various origins.


Step-by-Step Solution:

1) Call rewind(stream). 2) File position indicator becomes 0 (beginning of file). 3) Clear EOF and error flags for the stream. 4) No value is returned; to check errors you would subsequently use functions like ferror.


Verification / Alternative check:

Behavior mirrors fseek(stream, 0, SEEK_SET) followed by clearerr(stream), but rewind is the concise, standardized helper for this common action.


Why Other Options Are Wrong:

Character reverse: There is no such notion in standard C I/O.

End of file: rewind goes to the start, not the end.

Beginning of that line: Line concepts are not known to the stream positioning API.


Common Pitfalls:

  • Forgetting that rewind also clears EOF and error indicators.
  • Expecting a return value to signal success or failure.


Final Answer:

Reposition the file pointer to beginning of file.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion