tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: fopen(.., "a") + fseek

On Fri, Nov 07, 2008 at 12:13:26AM +0700, Robert Elz wrote:
 >   | Nope. I've opened the file with "a" which is for writing only. In
 >   | this light, the fseek "fails".
 > No, the fseek() works just fine - while in just "a" mode you can't use
 > fread() (or any other input function) to check that, you can still use
 > ftell().
 > However the fseek() doesn't fail, it is just mostly useless.   But being
 > useless isn't a reason for a function to return an error - you can't expect
 > the system, or libc, or other, functions to find every bug for you.

So, a mostly aimless standards-pedantic question: does the implicit
seek associated with append mode actually count as a call to a file
positioning function, allowing one to call fread() followed
immediately by fwrite()? C99 does not appear to say one way or the
other, but I haven't dug very deeply.

(And, FWIW, I'm pretty sure the historic behavior of stdio is that "a"
does not necessarily have this behavior; according to old security
lore you have to open() with O_APPEND if you really want to be sure
you're appending, e.g. in the presence of multiple writer processes.
If you just use fopen and "a" you might get just one seek to eof at
open time.)

David A. Holland

Home | Main Index | Thread Index | Old Index