다음을 통해 공유


_lseek, _lseeki64

지정된 위치로 파일 포인터를 이동합니다.

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

매개 변수

  • fd
    열려 있는 파일을 나타내는 파일 설명자입니다.

  • 오프셋
    원본의 바이트들의 수.

  • 원본
    초기 위치입니다.

반환 값

_lseek 은 바이트에서, 파일의 시작으로부터 새로운 위치의 오프셋을 반환합니다. _lseeki64 64 비트 정수 오프셋을 반환합니다. 함수는 오류를 표시하는 –1L을 반환 합니다. 잘못된 파일 설명자나 잘못된 원본 및 파일의 시작 전에 오프셋 으로 지정된 위치가 있는 것처럼 잘못된 매개변수가 전달된 경우, 매개 변수 유효성 검사 에 설명된 대로 잘못된 매개변수 처리기가 호출됩니다. 계속해서 실행하도록 허용된 경우, 이러한 함수는 EBADFerrno 를 설정하고 -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 );
}

입력: 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