Question Details

No question body available.

Tags

c cygwin posix standards c89

Answers (2)

Accepted Answer Available
Accepted Answer
March 1, 2026 Score: 5 Rep: 37,311 Quality: High Completeness: 70%

Is an implementation permitted to start reading too at the end of the file?

Yes.

From fopen(3) — manual page:

a+ ...
POSIX is silent on what the initial read position is when using this mode. For glibc, the initial file position for reading is at the beginning of the file, but for Android/BSD/MacOS, the initial file position for reading is at the end of the file.

(emphasis is mine)

Not only that reading from the start is not mandatory, there are systems which document that the initial position is at the end.
Cygwin is not mentioned, but it looks like it is a valid choice.

Therefore using fseek (like you did) is the correct approach.

March 1, 2026 Score: 3 Rep: 22,178 Quality: Medium Completeness: 60%

The semantics of "a+" mode isn't really the issue here. The semantics of files is the issue. The C24 Standard says (7.23.3):

A stream is associated with an external file... by opening a file, which can involve creating a new file.... 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. [emphasis mine]

I have quoted the current C Standard above, but the C89 Standard contains the same language.

In answer to the question of whether an implementation is permitted to start reading from the end of the file, the answer is "yes" because implementations are allowed to open files in append mode with the file position indicator at the end of the file. To reliably read from the start of a file opened in "a+" mode you need to explicitly set the file position indicator to the start of the file.

Should a conforming implementation read from the start and always write to the end?

After opening a file in append mode, a conforming implementation is not required to read from the start of the file. But, with a caveat, a conforming implementation is required to write to the end of the file (C24 7.23.5.3):

Opening a file with append mode... causes all subsequent writes to the file to be forced to the then current end-of-file at the point of buffer flush or actual write, regardless of intervening calls to the fseek, fsetpos, or rewind functions.

But the same section of the Standard contains a caveat (7.23.5.3):

When a file is opened with update mode (+ as the second or third character in the previously described list of mode argument values), both input and output can be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file. [emphasis mine]

The quotes above are again from the current C Standard, and again similar language is contained in the C89 Standard.

If you open a file with "a+" and attempt to read from the file first, a conforming implementation is not required to write to the end of the file unless either: 1) end-of-file was encountered during the read operation, or 2) an intervening call to a file positioning function was made before the write operation. In fact, attempting to write to such a file stream after reading from it without an intervening positioning call (or end-of-file condition) causes undefined behavior.