共用方式為


_lseek _lseeki64

將檔案指標移至指定的位置。

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

參數

  • fd
    指的已開啟檔案的檔案描述項。

  • 位移
    中的位元組數字原點

  • 原點
    初始位置。

傳回值

_lseek會傳回位移,以位元組為單位的檔案從一開始新的位置。 _lseeki64在 64 位元整數傳回的位移。 函式會傳回代表錯誤發生的 –1L。 如果傳遞了無效的參數,例如錯誤的檔案描述項或值原點是不正確或所指定的位置位移是在開始之前的檔案,不正確的參數處理常式會叫用,如所述參數驗證。 如果執行,則允許繼續執行,這些函式會設定errnoEBADF ,並傳回-1 L。 在裝置上的搜尋 (例如終端機和印表機) 觀念,傳回的值未定義。

如需有關這些及其他錯誤碼的詳細資訊,請參閱 _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 );
}

輸入: 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