kBisla
kBisla

Reputation: 640

How can I check the current offset position in a file using lseek?

Am traversing a text file from the end to the start using lseek() in linux. How can I check if the offset position has reached the start of the file.

PS am not looking for a solution using the file size to keep track of the offset manually.

Upvotes: 5

Views: 10644

Answers (2)

Malcolm Rowe
Malcolm Rowe

Reputation: 777

I'm guessing you're using SEEK_CUR with a negative offset. If you want to find out where you are after it returns, use the return value of lseek() directly.

From man lseek:

Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the beginning of the file.

Upvotes: 2

Alex MDC
Alex MDC

Reputation: 2456

Doesn't lseek() return the file offset from the beginning of the file? You can keep checking the return and once it returns 0, you are at the start of the file. Just be careful that -1 return value indicates an error.

Man page: http://linux.die.net/man/2/lseek

Upvotes: 1

Related Questions