read after lseek always return 0

Posted by Wins on Stack Overflow See other posts from Stack Overflow or by Wins
Published on 2012-06-10T03:46:04Z Indexed on 2012/06/10 4:40 UTC
Read the original article Hit count: 85

Filed under:
|
|
|

I'm having an issue reading byte in file after performing lseek

long cur_position = lseek(fd, length * -1, SEEK_CUR ); // Rewind file reading by length bytes.
printf("cur_position: %i\n", cur_position);
int num_byte = read(fd, &byte, 1);
printf("num_byte = %i\n", num_byte);

With the initial variable length set to 34, the above code would produce cur_position 5 (so there are definitely at least 34 bytes after the lseek function returns), but the variable num_byte returned from function read always returns 0 even though there are still more bytes to read.

Does anyone know the reason num_byte always return 0 or do I make mistake in the above code?

Just for information, the above code was run on the following machine

$ uname -srvpio
Linux 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 GNU/Linux

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux