_lseek, _lseeki64

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

구문

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

매개 변수

fd
열려 있는 파일을 참조하는 파일 설명자입니다.

offset
origin부터의 바이트 수입니다.

origin
초기 위치입니다.

반환 값

_lseek는 파일의 시작 부분으로부터 새 위치의 오프셋을 바이트 단위로 반환합니다. _lseeki64는 64비트 정수로 오프셋을 반환합니다. 함수는 오류를 나타내기 위해 -1L을 반환합니다. 잘못된 파일 설명자와 같은 잘못된 매개 변수를 전달했거나 origin 값이 잘못되었거나 지정한 offset 위치가 파일 시작 전인 경우 매개 변수 유효성 검사에 설명된 대로 잘못된 매개 변수 처리기가 호출됩니다. 계속해서 실행하도록 허용된 경우 이러한 함수는 errnoEBADF로 설정하고 -1L을 반환합니다. 검색을 수행할 수 없는 디바이스(예: 터미널 및 프린터)에서는 반환 값이 정의되지 않습니다.

이러한 오류 코드 및 기타 오류 코드에 대한 자세한 내용은 , _doserrno_sys_nerr_sys_errlist를 참조하세요.errno

설명

이 함수는 _lseek 연결된 fd 파일 포인터를 바이트인 origin새 위치 offset 로 이동합니다. 파일에 대한 다음 작업은 새 위치에서 수행됩니다. 인수는 origin Stdio.h에 정의된 다음 상수 중 하나여야 합니다.

origin 설명
SEEK_SET 파일 시작
SEEK_CUR 파일 포인터의 현재 위치
SEEK_END 파일 끝

_lseek를 사용하여 파일의 아무 위치나 파일의 끝을 지난 위치로 포인터의 위치를 변경할 수 있습니다.

기본적으로 이 함수의 전역 상태는 애플리케이션으로 범위가 지정됩니다. 이 동작을 변경하려면 CRT의 전역 상태를 참조하세요.

요구 사항

루틴에서 반환된 값 필수 헤더
_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.

출력

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