Carefully return value lseek

Reprinted from: http://book.51cto.com/art/201701/528116.htm

For most of the Linux system call, if the return value is negative, then it is usually wrong, but it is for lseek this rule does not apply. Let us look at the lseek return value:

When lseek is successful, it returns the offset position to file the final starting position as a starting point. If an error occurs, it returns -1 errno is set to correspond to an error value.

That is, in general, ordinary files, lseek returns are non-negative integers, but for some devices files, it is allowed to return a negative offset. To determine whether it is truly so lseek error, errno must be reset to 0 before calling lseek, then call lseek, while checking the return value is a value of -1 and the errno. Only when the two set up at the same time, it indicates the lseek really wrong.

Because the file offset here are the core of the concept, so lseek does not cause any real I / O operations.

Note: For ftruncate other functions still apply

Guess you like

Origin www.cnblogs.com/csstudy/p/11547394.html