ftell() oddity in 'append' mode

Moderator
Posts: 691
Joined: 2002.04
Post: #1
I'm coding a jump-table for a level file format, and I've encountered an oddity with calls to ftell() on a file opened in 'append' mode; whilst writes to the file function correctly, appending to the end of the file, the calls to ftell() always return 0...

Code:
FILE *f = fopen( p, "ab" );
printf( "%ld\n", ftell( f )); // Returns 0, no matter the file length.

I'm able to circumvent the problem by making what should be a no-op call to fseek() prior to the call to ftell()...

Code:
FILE *f = fopen( p, "ab" );
fseek( file, 0, SEEK_END );
printf( "%ld\n", ftell( f )); // Returns the file length

...but I'm wondering if this is the expected behaviour of ftell() in this situation? (None of the documentation I've found states that there should be any difference in behaviour between calls to ftell() on a file opened in 'append' mode and any other.)

Mark Bishop
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Maybe it's because nobody in their right mind would use "a" Rasp
Quote this message in a reply
Moderator
Posts: 691
Joined: 2002.04
Post: #3
OneSadCookie Wrote:Maybe it's because nobody in their right mind would use "a" Rasp

Could you elaborate? Huh

Mark Bishop
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
"a" has very odd semantics (the file pointer always points to the end of the file, regardless of how you seek). "r+" is much more generally useful.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Button Title Oddity Justin Brimm 2 2,206 Mar 18, 2006 05:08 PM
Last Post: Justin Brimm