通过man 2 lseek可以查看linux中的系统函数lseek函数的帮助文档,为了更好的学习,我把这些重要内容翻译过来
1 NAME 2 lseek - reposition read/write file offset//重置读或写文件的偏移量 3 4 SYNOPSIS//摘要 5 #include <sys/types.h>//如果要使用lseek函数,需要包含这两个头文件 6 #include <unistd.h> 7 8 off_t lseek(int fd, off_t offset, int whence);//lseek的声明格式 9 10 DESCRIPTION//描述 11 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: 13 //lseek()函数的作用是,重置和文件描述符fd关联的打开的文件的偏移量,把这个偏移量重置为参数offset,在重置时要根据指令whence来做。whence的可包含的指令如下: 14 SEEK_SET 15 The offset is set to offset bytes.//如果whence的指令是SEEK_SET,则文件偏移量设定为参数offset大小的字节 16 17 SEEK_CUR 18 The offset is set to its current location plus offset bytes.//如果whence的指令是SEEK_CUR,则文件偏移量设定为当前位置加上offset大小的字节 19 20 SEEK_END 21 The offset is set to the size of the file plus offset bytes.//如果whence的指令是SEEK_END,则文件偏移量设定为文件大小再加上offset大小的字节 22 23 The lseek() function allows the file offset to be set beyond the end of the file (but this does not change the 24 size of the file). If data is later written at this point, subsequent reads of the data in the gap (a "hole") 25 return null bytes (‘\0‘) until data is actually written into the gap.//lseek()函数允许把文件的偏移量设置的值超过文件的结束(虽然把偏移量设定的值超过了文件的结尾,但是并不会改变文件大小)。如果数据后来被写入到这个偏移量位置,在把数据真正写入这个从文件末尾到偏移量之间的空间,那么随后在读取从文件末尾到设置的偏移量的位置,返回的都是‘\0’, 26 RETURN VALUE 27 Upon successful completion, lseek() returns the resulting offset location as measured in bytes from the begin‐ 28 ning of the file. On error, the value (off_t) -1 is returned and errno is set to indicate the error.//当lseek成功完成,lseek()返回的偏移量是距离文件开头的偏移量,大小以自己为单位衡量。如果错误,返回值为-1,并设置errno的值
时间: 2024-10-13 01:05:56