Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <b>lseek() reposition file pointer to specified location</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 lseek()                 Reposition File Pointer to Specified Location

 #include   <io.h>                       Required for declarations only

 long       lseek(handle,offset,origin);
 int        handle;                      Open file handle
 long       offset;                      Number of bytes from origin
 int        origin;                      Reference point of the seek

    lseek() moves the file pointer associated with 'handle' to a new
    location, which can be anywhere in the file. The location is 'offset'
    bytes from the 'origin'. 'origin' can be one of the following
    constants:

            SEEK_SET  (0)   Beginning of file
            SEEK_CUR  (1)   Current pointer position
            SEEK_END  (2)   End of file

    lseek() can be used to reposition the pointer beyond the end of the
    file.  Trying to reposition the pointer before the beginning of the
    file, however, causes an error.

       Returns:     The offset of the pointer's new position, specified
                    in bytes from the beginning of the file. On error,
                    lseek() returns -1L, and 'errno' is set to one of the
                    following:

                    EBADF:      Invalid file handle
                    EINVAL:     Offset is before the beginning of the file


         Notes:     The return value is undefined if the device is
                    incapable of seeking (for example, it is a terminal
                    or a printer).

                    Use tell() to read the current position of the file
                    pointer.

   -------------------------------- Example ---------------------------------

    The following statements open a file, move the file pointer to the
    end of the file, and write at that position.

           #include <io.h>
           #include <fcntl.h>
           #include <stdlib.h>

           int fhndl;
           char buff[10000];
           long position;

           main()
           {
               fhndl = open("invoice.dat",O_RDWR);
               position = lseek(fhndl,0L,SEEK_END);
               if (position != -1)
                  write(fhndl,buff,10);
           }


See Also: fseek() tell() ftell()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson