RAD Studio |  |

lseek

Header File

io.h

Category

Input/output Routines

Prototype

long lseek(int handle, long offset, int fromwhere);

Description

Moves file pointer.

lseek sets the file pointer associated with 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):

SEEK_CUR | Current file pointer position
SEEK_END | End-of-file
SEEK_SET | File beginning

Return Value

lseek returns the offset of the pointer’s new position measured in bytes from the file beginning. lseek returns -1L on error, and the global variable errno is set to one of the following values:

EBADF | Bad file handle
EINVAL | Invalid argument
ESPIPE | Illegal seek on device

On devices incapable of seeking (such as terminals and printers), the return value is undefined.

Example

Copy Code

    #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++
+ | + |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!