Share this page

# fseek

Input-Output Functions

Sets the current position in a file to a new location.

    int fseek( FILE *file_pointer, long offset, int origin);

file_pointer | A pointer to a file.
offset | The offset within the file, in bytes.
origin | The starting point. Use one of the following values: SEEK_SET (0) Beginning of file. SEEK_CUR (1) Current position of the file pointer. SEEK_END (2) End of file.

For fseek details, refer to your C language documentation.

## Return Values

If successful, the function returns 0. Otherwise it returns non-zero.

## Example

This example creates a log file and writes the following to it:

- the string "Vuser"

- the integer "Vuser ID"

- the time, as returned by the time function.

To read these values back again, the file cursor is moved to the beginning of the file, using fseek.

        #include <stdio.h>

        #include <time.h>
        #ifdef unix
            char * filename = "/tmp/logfile.txt";
        #else
            char * filename = "c:\\logfile.txt";
        #endif
        FILE* file, thetime;
        int id, id2;
        time_t t;
        char s[32];

    // Create a new file

        if ((file = fopen(filename, "w+" )) == NULL) {
            lr_output_message ("Unable to create %s", filename);
            return -1;
        }

        // Write the Vuser ID and time

        lr_whoami (&id, NULL, NULL);
        fprintf(file, "Vuser %d %ld", id, time(&t));

    // Now read what we've just written. Rewind to start of file

        fseek(file, 0, 0); // 0 is SEEK_SET
        fscanf(file, "%s", &s);
        fscanf(file, "%d", &id2);
        fscanf(file, "%ld", &thetime);
        lr_output_message ("%s %d %ld", s, id2, thetime);
        fclose(file);

    Example: Output:
    Vuser -1 1020675497

Tips to improve your search results... VuGen Function Reference Version 26.1 Function Reference 26.1 All View Local Help Center View Online Help Center Version: