Understanding lseek

I tried to use lseek system call to determine the number of bytes in a file. To do so, I used open system call with O_APPEND flag to open a file. As lseek returns the current offset so I called lseek for opened file with offset as zero and whence as SEEK_CUR. So I guess it must return the number of bytes as the file is ready to append and lseek seeks only 0 bytes. But result is showing 0 bytes. Please correct me if I have understood wrong.

Program is as following

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fnctl.h>

int main(void)
{
   int fd;
   
   if((fd = open("./example.txt", O_RDWR | O_APPEND)) < 0) {
       perror("File open: ");
       exit(1);
   }
   printf("File size in bytes: %ld\n", lseek(fd, 0, SEEK_CUR));
   close(fd);

   exit(0);
}

Program gives the output as zero, though the file 'example.txt' do exist with so data in it.

Thanks.

File pointers do not necesarily have any relevance to file size. Use fstat() on an open file. lseek() is meant to move file pointers, other uses may not work as you found out.

#include <sys/stat.h>
size_t filesize(int fd)
{
    struct stat st;
    if(fstat(fd, &st)==-1)
    {
        perror("Cannot stat file");
        exit(1);
    }
    return st.st_size;
}

// usage someplace else in your code

FILE *in=fopen(somefile.dat, "a");
size_t sz=filesize(fileno(in));

Your entire line of reasoning depends on the open() syscall placing the file pointer at the end of the file. That is not a given.

From the POSIX open() system call manual page

Additionally, keep in mind that writes from other processes won't adjust your descriptor's offset.

Regards,
Alister

---------- Post updated at 02:32 PM ---------- Previous update was at 02:18 PM ----------

That's not a safe use of printf. You're assuming that off_t and long are the same width. I suggest something like:

printf("lseek return value: %jd\n", (intmax_t) lseek(fd, 0, SEEK_CUR));

Regards,
Alister

Thanks Jim. It's an nice and simple idea for identifying the file size.

---------- Post updated at 02:17 PM ---------- Previous update was at 01:19 PM ----------

Thanks Alister

That was helpful. Open syscall used with O_APPEND option doesn't position the offset to end of the file. Rather offset is positioned to the 'seeked' place just before any write operation occurs. This is what I observed with with the following code.

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

int main(void)
{
int fd;

if((fd = open("./example.c", O_WRONLY | APPEND)) < 0) {
perror("Open failed: ");
exit(1);
}
printf("Offset after append flag: %jd\n", (intmax_t) lseek(fd, 0, SEEK_CUR));
if(write(fd, "end", 3) != 3)
perror("Write failed: ");
printf("Offset after write: %jd\n", (intmax_t) lseek(fd, 0, SEEK_CUR));
close(fd);

exit(0);
}

And Thanks for correcting me with format specifier %jd and type conversion of lseek's return value. It helps with compatibility issue, that's what I found out. It will be appreciable if you can explain it.

Regards
Deepak

---------- Post updated at 02:24 PM ---------- Previous update was at 02:17 PM ----------

Hi

Okay, now I thought of a way to use lseek to know number of bytes in file. I only need to simply replace whence SEEK_CUR to SEEK_END. I guess, it works because we can't make sure that offset is set to EOF using O_APPEND, but SEEK_END with zero as offset will set it to EOF, surly. Correct me if I'm incorrect.

Regards
Deepak