bit_by_bit
bit_by_bit

Reputation: 337

fseek would not return the expected location

I have a function where I am trying to reach the last index of the file contents. When i use descriptors and lseek combination as given below, things work normally:

offset = lseek(infd, 0, SEEK_END);

results in offset: 39 (which byte size of the file)

infd is 3

offset = fseek(file1, 0, SEEK_END);

results in offset 0

file1 is pointing to 0x00007fff8897e320

Why is the offset resulting in 0 when I try to use a file pointer to the same?

Note: offset is of type off_t

Upvotes: 0

Views: 152

Answers (1)

Gal Birka
Gal Birka

Reputation: 591

You are using fseek, if you read the documentation you can find:

Return value

​0​ upon success, nonzero value otherwise.

On the other hand, lseek documentation reads:

Return value

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

Hope this clears thing up, and next time I really advise you to read the reference and check for return value section!

Upvotes: 4

Related Questions