lseek
Go Up to io.h Index
Header File
io.h
Category
Input/output Routines
Prototype
long lseek(int handle, long offset, int fromwhere); __int64 _lseeki64(int handle, __int64 offset, int fromwhere);
Description
Moves file pointer.
lseek sets the file pointer associated with the handle to a new position offset bytes beyond the file location given by fromwhere. fromwhere must be one of the following symbolic constants (defined in io.h):
|
|
Current file pointer position |
|
|
End-of-file |
|
|
File beginning |
Return Value
lseek returns the offset of the pointer's new position measured in bytes from the beginning of the file. lseek returns -1L on error, and the global variable errno is set to one of the following values:
|
|
Bad file handle |
|
|
Invalid argument |
|
|
Illegal seek on device |
On devices incapable of seeking (such as terminals and printers), the return value is undefined.
Example
#include <sys\stat.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
int main(void)
{
int handle;
char msg[] = "This is a test";
char ch;
/* create a file */
handle = open("TEST.$$$", O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
/* write some data to the file */
write(handle, msg, strlen(msg));
/* seek to the beginning of the file */
lseek(handle, 0L, SEEK_SET);
/* reads chars from the file until we hit EOF */
do
{
read(handle, &ch, 1);
printf("%c", ch);
} while (!eof(handle));
close(handle);
return 0;
}
Portability
| POSIX | Win32 | ANSI C | ANSI C++ |
|---|---|---|---|
|
+ |
+ |