Share via


_lseek、_lseeki64

将文件指针移到指定位置。

long _lseek( 
   int fd, 
   long offset, 
   int origin  
); 
__int64 _lseeki64( 
   int fd, 
   __int64 offset, 
   int origin  
);

参数

  • fd
    引用开启文件的描述符。

  • 偏移量
    原始字节数。

  • 原始
    初始位置。

返回值

_lseek 返回字节偏移量,从开头文件的新位置。 _lseeki64 返回在 64 位整数的偏移量。 函数返回 –1L 指示错误。 如果传递的参数无效,例如一个坏文件说明符或 原始 值无效或 偏移所 指定的位置是,在文件的开头,无效参数调用处理程序之前,如 参数验证所述。 如果允许执行继续,则这些功能将 errno 设置为 EBADF,并返回 -1L。 在设备上不能查找 (如运行和打印机),返回值是不确定的。

有关这些属性和其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerr

备注

_lseek 函数移动了文件指针与 fd 到偏离原点偏移量 为字节的新位置。 文件的下一操作在新位置。 原点 参数必须是下列常数之一,在 Stdio.h 定义。

  • SEEK_SET
    文件名的开头。

  • SEEK_CUR
    文件指针的当前位置。

  • SEEK_END
    文件尾。

可以使用 _lseek 重新定位指针在文件中的任意位置或除文件外的末尾。

要求

例程

必需的标头

_lseek

<io.h>

_lseeki64

<io.h>

有关更多兼容性信息,请参见“简介”中的兼容性

C 运行时库的所有版本。

示例

// crt_lseek.c
/* This program first opens a file named lseek.txt.
 * It then uses _lseek to find the beginning of the file,
 * to find the current position in the file, and to find
 * the end of the file.
 */

#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <share.h>

int main( void )
{
   int fh;
   long pos;               /* Position of file pointer */
   char buffer[10];

   _sopen_s( &fh, "crt_lseek.c_input", _O_RDONLY, _SH_DENYNO, 0 );

   /* Seek the beginning of the file: */
   pos = _lseek( fh, 0L, SEEK_SET );
   if( pos == -1L )
      perror( "_lseek to beginning failed" );
   else
      printf( "Position for beginning of file seek = %ld\n", pos );

   /* Move file pointer a little */
    _read( fh, buffer, 10 );

   /* Find current position: */
   pos = _lseek( fh, 0L, SEEK_CUR );
   if( pos == -1L )
      perror( "_lseek to current position failed" );
   else
      printf( "Position for current position seek = %ld\n", pos );

   /* Set the end of the file: */
   pos = _lseek( fh, 0L, SEEK_END );
   if( pos == -1L )
      perror( "_lseek to end failed" );
   else
      printf( "Position for end of file seek = %ld\n", pos );

   _close( fh );
}

Input: crt_lseek.c_input

Line one.
Line two.
Line three.
Line four.
Line five.

Output

Position for beginning of file seek = 0
Position for current position seek = 10
Position for end of file seek = 57

请参见

参考

低级别 I/O

fseek、_fseeki64

_tell、_telli64