Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <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
 #include   <stdio.h>

 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() repositions the file pointer associated with 'handle', moving
    it 'offset' bytes from the original location, 'origin'. 'origin' can
    be one of the following constants (defined in <stdio.h>):

            SEEK_SET     Beginning of file
            SEEK_CUR     Current pointer position
            SEEK_END     End of file

    The pointer can be repositioned anywhere in the file.  It can also be
    moved beyond the end of the file.  The opposite tack, however--trying
    to reposition the pointer before the beginning of the file--produces
    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

                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.

 Portability:   Not supported by ANSI standard.

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