Email List: Xaustin-group-lX
[All Lists]

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

To: yyyyy@xxxxxxxxx (Clive D.W. Feather)
Subject: Re: fopen(..., "a+")
From: "Wojtek Lerch" <yyyyyy@xxxxxxx>
Date: Wed, 13 Aug 2003 12:55:56 -0400 (EDT)
Cc: yyyyyy@xxxxxxxxxxxxxxxxxxx (Harti Brandt), yyyyyyyyyyyyyy@xxxxxxxxxxxxx
Clive D.W. Feather wrote:
> It isn't clear to me from the C Standard whether a write to an append-only
> file affects the file position indicator and, if so, how.

7.19.8.2p2 seems pretty clear about fwrite():

    The file position indicator for the stream (if defined) is advanced
    by the number of characters successfully written.

And since 7.19.5.3p5 says that the append mode forces writes to go to
the end of file but not that the current position goes there, too, it
seems to mean that the following program must output "6" if everything
succeeds:

  #include <stdio.h>

  int main( void ) {
      FILE *fp = fopen( "file", "ab" );
      if ( fp && fwrite( "Hello", 1, 5, fp ) == 5 ) {
          rewind( fp );
          if ( fwrite( " world", 1, 6, fp ) == 6 )
              printf( "%ld\n", ftell(fp) );
          }
      } 

And it indeed does output "6" on my system -- I have to admit it's not
what I expected...



<Prev in Thread] Current Thread [Next in Thread>