lseek()函数

时间:2023-03-10 02:16:18
lseek()函数

lseek()有个特殊的用途,确定文件是常规文件还是设备。
<pre lang="c" escaped="true">
off_t currpos;
ourrpos = lseek(fd, 0, SEEK_CUR);
if (ourrpos == -1)
{
printf("this is drive file");
}
</pre>
这种方法用来确定文件或者设备是否可以设置偏移量,常规文件都可以设置偏移量,而设备一般是不可以设置偏移量的,如果设备不支持lseek(),则lseek返回-1, 并将errno设备为ESPIPE.

<pre lang="c" escaped="true">
#include <sys/types.h>
#include <unistd.h>

off_t lseek(int fd, off_t offset, int whence);

DESCRIPTION
The lseek() function repositions the offset of the open file associated with the file descriptor fd to the argument offset according to the directive whence as
follows:

SEEK_SET
The offset is set to offset bytes.

SEEK_CUR
The offset is set to its current location plus offset bytes.

SEEK_END
The offset is set to the size of the file plus offset bytes.

</pre>