_lseek, _lseeki64

Memindahkan penunjuk file ke lokasi yang ditentukan.

Sintaks

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

Parameter

fd
Pendeskripsi file yang mengacu pada file yang terbuka.

offset
Jumlah byte dari origin.

origin
Posisi awal.

Nilai hasil

_lseek mengembalikan offset, dalam byte, dari posisi baru dari awal file. _lseeki64 mengembalikan offset dalam bilangan bulat 64-bit. Fungsi mengembalikan -1L untuk menunjukkan kesalahan. Jika melewati parameter yang tidak valid, seperti deskriptor file yang buruk, atau nilai untuk origin tidak valid atau posisi yang ditentukan sebelum offset awal file, handler parameter yang tidak valid dipanggil, seperti yang dijelaskan dalam Validasi parameter. Jika eksekusi diizinkan untuk melanjutkan, fungsi-fungsi ini diatur errno ke EBADF dan mengembalikan -1L. Pada perangkat yang tidak dapat mencari (seperti terminal dan printer), nilai pengembalian tidak terdefinisi.

Untuk informasi selengkapnya tentang kode kesalahan ini dan lainnya, lihat errno, , _doserrno_sys_errlist, dan _sys_nerr.

Keterangan

Fungsi memindahkan _lseek penunjuk file yang terkait dengan fd ke lokasi baru yaitu offset byte dari origin. Operasi berikutnya pada file terjadi di lokasi baru. Argumen origin harus merupakan salah satu konstanta berikut, yang didefinisikan dalam Stdio.h.

origin nilai Deskripsi
SEEK_SET Awal file.
SEEK_CUR Posisi penunjuk file saat ini.
SEEK_END Akhir file.

Anda dapat menggunakan _lseek untuk memposisikan ulang pointer di mana saja dalam file atau di luar akhir file.

Secara default, status global fungsi ini dicakup ke aplikasi. Untuk mengubah perilaku ini, lihat Status global di CRT.

Persyaratan

Rutin Header yang diperlukan
_lseek <io.h>
_lseeki64 <io.h>

Untuk informasi kompatibilitas selengkapnya, lihat Kompatibilitas.

Pustaka

Semua versi pustaka run-time C.

Contoh

// 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.

Hasil

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

Baca juga

I/O tingkat rendah
fseek, _fseeki64
_tell, _telli64