Every disk file has a corresponding file pointer that marks the current location in the file. The current location is the byte in the file that will be read from or written to on the next call to DosRead or DosWrite. Usually, the file pointer is at the beginning of the file when you first open or create the file, and it advances as you read or write to the file. You can, however, change the position of the file pointer at any time by using DosSetFilePtr.

DosSetFilePtr moves the file pointer a specified offset from a given position. You can move the pointer from the beginning of the file, from the end, or from the current position.

The following code fragment shows how to move the pointer 200 bytes from the end of the file:

    #define INCL_DOSFILEMGR   /* File system values     */
    #include <os2.h>

    #define HF_STDOUT 1       /* Standard output handle */
    #define BUF_SIZE 255

    BYTE abBuf[BUF_SIZE];
    HFILE hf;
    ULONG ulRead,
          ulWritten,
          ulAction,
          ulNewPtr;

    DosOpen("SAMPLE.TXT",
            &hf,
            &ulAction,
            0,
            FILE_NORMAL,
            FILE_OPEN,
            OPEN_ACCESS_READONLY |
            OPEN_SHARE_DENYNONE,
            (PEAOP2) NULL);

    DosSetFilePtr(hf,
                  -200,
                  FILE_END,
                  &ulNewPtr);

    DosRead(hf,
            &abBuf,
            sizeof(abBuf),
            &ulRead);

    DosWrite(HF_STDOUT,
             abBuf,
             ulRead,
             &ulWritten);

In this example, DosSetFilePtr moves the file pointer to the 200th byte from the end of the file. If the file is not that long, the function moves the pointer to the first byte in the file and returns the actual position (relative to the end of the file) in the ulNewPtr variable.

You can move the file pointer for disk files only. You cannot use DosSetFilePtr on devices, despite using other file functions (DosOpen, DosRead) to access a device.

If a file is read-only, write operations to the file will not be performed.

Moving the pointer from the end of the file can be used to determine the size of the file.


[Back] [Next]