This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
fopen() behavior: File positioning in append mode
- From: Stefan Heinzmann <stefan_heinzmann at gmx dot de>
- To: "'newlib at sourceware dot org'" <newlib at sourceware dot org>
- Date: Fri, 3 Feb 2017 11:59:23 +0100
- Subject: fopen() behavior: File positioning in append mode
- Authentication-results: sourceware.org; auth=none
Hi,
I am unsure what the behavior of fopen() should be when the "a+" mode is
given. The source for this is in file newlib/libc/stdio/fopen.c
Mode a+ translates to open() flags O_RDWR|O_CREAT|O_APPEND according to
the POSIX standard. This would set the file position to the beginning of
the file. The file position would be relevant only to read operations,
since the O_APPEND flag causes writes to happen at the file end in any case.
However, fopen() seems to set the file position to the end in this case,
by calling fseek() explicitly. The consequence is that read operations
also happen at the file end. I don't understand why this is done.
Wouldn't it be more useful to omit the call to fseek(), or to restrict
it to the write-only case?
The C standard doesn't seem to be clear here. Does it actually specify
what the result of ftell() should be immediately after calling fopen()?
I seem to be unable to find conclusive text.
Thanks
Stefan