I have been looking through the documentation of fopen
and ifstream
, and what I cannot find is whether I need to seek to the beginning of a file after opening it. I think I can assume I don't need to, and experiments I have done support this hypothesis, but I find it safer to check.
CodePudding user response:
So for a file opened for "r"
/ in
, the answer is no, you do not need to seek to the start. Clearly it makes no sense to start from anywhere else on a file opened for read-only.
CodePudding user response:
No, the initial file position indicator will be positioned at the start of the file.
Per 7.21.3 Files, paragraph 1 of the (draft) C11 standard (bolding mine):
A stream is associated with an external file (which may be a physical device) by opening a file, which may involve creating a new file. Creating an existing file causes its former contents to be discarded, if necessary. If a file can support positioning requests (such as a disk file, as opposed to a terminal), then a file position indicator associated with the stream is positioned at the start (character number zero) of the file, unless the file is opened with append mode in which case it is implementation-defined whether the file position indicator is initially positioned at the beginning or the end of the file. The file position indicator is maintained by subsequent reads, writes, and positioning requests, to facilitate an orderly progression through the file.
Note that if the file is opened in append mode, the initial position is implementation-defined.
CodePudding user response:
You do not need to seek, as reading will begin at the start of the file by default.