lseek( ) on file opened with O_APPEND

I noticed that when I opened a file using open( ) with the O_APPEND
flag that I have to
first write to the file in order to have lseek( ) return the offset
value equivalent to the offset
from the beginning of the file.  For example, the code below

    fd = open( my_file, O_APPEND | O_WRONLY );
    printf( "Current offset following lseek( fd, O, SEEK_CUR): %d\n",
        ( int ) lseek( fd, 0, SEEK_CUR ) );

will print the text

Current offset following lseek( fd, O, SEEK_CUR): 0

If I write to the file and then call lseek( fd, 0, SEEK_CUR ), I will
get the actual offset from
the beginning of the file

    fd = open( my_file, O_APPEND | O_WRONLY );
    printf( "Current offset following lseek( fd, O, SEEK_CUR): %d\n",
        ( int ) lseek( fd, 0, SEEK_CUR ) );
    bytes_written = write( fd, buffer, num_bytes );
    init_off = lseek( fd, 0, SEEK_CUR ) - (off_t ) bytes_written;
    printf( "Actual initial offset to end of file: %d\n", init_off );
    printf( "Current file offset: %d\n", ( int ) lseek( fd, 0,
SEEK_CUR ) );

will give me output 
 
Current offset following lseek( fd, 0, SEEK_CUR): 0
Actual initial offset to end of file: 12
Current file offset: 23  

Note that this suggests that the initial file size was 12 bytes, and
11 bytes were written to
fd.

In summary, lseek( fd, 0, SEEK_CUR ) will return (off_t ) 0 after a
file is opened for
appending.  Once at least one byte is written to fd, lseek( fd, 0,
SEEK_CUR ) will
return the offset from the beginning of the file.  Is there a better
way to get the offset to
the end of the file?  I could have opened the file without the
O_APPEND flag, and called
lseek( fd, 0, SEEK_END ), but this not safe if multiple processes are
appending to the same
file.  By the way, forgive my lack of error checking above; I have
omitted it for the sake of
clarity in this post, not in the code itself.

Thanks for your help,
Victor
0
weinstev
11/18/2003 7:55:37 PM
comp.unix.programmer 10813 articles. 0 followers. kokososo56 (349) is leader. Post Follow

1 Replies
1376 Views

Similar Articles

[PageSpeed] 57

In article <4c9cde18.0311181155.3a092f44@posting.google.com>,
Victor <weinstev@yahoo.com> wrote:
>In summary, lseek( fd, 0, SEEK_CUR ) will return (off_t ) 0 after a
>file is opened for
>appending.  Once at least one byte is written to fd, lseek( fd, 0,
>SEEK_CUR ) will
>return the offset from the beginning of the file.  

O_APPEND causes you to go to the end immediately before writing anything.
Until you try to write something, it doesn't have to reposition.

>						    Is there a better
>way to get the offset to
>the end of the file?  

Call fstat().

>		       I could have opened the file without the
>O_APPEND flag, and called
>lseek( fd, 0, SEEK_END ), but this not safe if multiple processes are
>appending to the same
>file.

If other processes are appending to the file, any method you use is
useless, since the file length can change after you call it.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
0
Barry
11/18/2003 8:53:40 PM
Reply: