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>fseek() reposition file pointer to given location</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 fseek()                 Reposition File Pointer to Given Location

 #include   <stdio.h>

 int        fseek(stream,offset,origin);
 FILE       *stream;                     Pointer to file structure
 long       offset;                      Number of bytes from origin
 int        origin;                      Initial position

    fseek() moves the file pointer associated with 'stream' to 'offset'
    number of bytes from the 'origin'. After an fseek(), the next
    operation can be either a read or write, depending on how the file
    was opened; the operation takes place at the new position.  'origin'
    is one of the following constants (defined in <stdio.h>):

               SEEK_SET           Beginning of file

               SEEK_CUR           Current position of file pointer

               SEEK_END           End of file

    SEEK_SET, SEEK_CUR, and SEEK_END are equivalent to the values 0, 1,
    and 2, respectively.

       Returns:     0, if successful.  On error, a non-zero value is
                    returned.  The return value is undefined on devices
                    incapable of seeking (terminals and printers).

         Notes:     fseek() can be used to reposition the pointer
                    anywhere in or beyond the end of a file.  Positioning
                    it before the beginning of a file causes an error.

                    Because of the CR-LF translations performed when a
                    file is opened in text mode, the only fseek()
                    operations guaranteed to work on streams open in text
                    mode are:

                    o seeking with an offset of 0 relative to any of the
                    original values,

                    o seeking from the beginning of a file with a offset
                    value returned from a call to ftell().

                    fseek() discards any character pushed back by using
                    ungetc().

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

    The following statements move the file pointer to the end of the
    file.

           #include <stdio.h>

           FILE *stream;
           int rslt;

           main()
           {
               if ((stream = fopen("data.txt","r+")) != NULL) {
                    rslt = fseek(stream, 0L, SEEK_END);
               }
           }



See Also: ftell() lseek() rewind()

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