I got confused about lseek()
's return value(which is new file offset)
I have the text file (Its name is prwtest). Its contents are written to a to z.
And, the code what I wrote is following,
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 #define BUF 50
8
9 int main(void)
10 {
11 char buf1[]="abcdefghijklmnopqrstuvwxyz";
12 char buf2[BUF];
13 int fd;
14 int read_cnt;
15 off_t cur_offset;
16
17 fd=openat(AT_FDCWD, "prwtest", O_CREAT | O_RDWR | O_APPEND);
18 cur_offset=lseek(fd, 0, SEEK_CUR);
19 //pwrite(fd, buf1, strlen(buf1), 0);
20 //write(fd, buf1, strlen(buf1));
21 //cur_offset=lseek(fd, 0, SEEK_END);
22
23 printf("current offset of file prwtest: %d \n", cur_offset);
24
25 exit(0);
26 }
On the line number 17
, I use flag O_APPEND
, so the prwtest's current file offset is taken from i-node's current file size. (It's 26).
On the line number 18
, I use lseek()
which is used by SEEK_CUR, and the offset is 0.
But the result value cur_offset
is 0. (I assume that it must be 26, because SEEK_CUR indicates current file offset.)
However, SEEK_END
gives me what I thought, cur_offset
is 26.
Why the lseek(fd, 0, SEEK_CUR);
gives me return value 0, not 26?