fgetpos()

Declaration

int fgetpos(FILE* qpStream, fpos_t* qpPos);

Description:

This function sets the object pointed to by "qpPos" to the file-position indicator of "qpStream." The function returns zero if it was successful and a nonzero value otherwise. If the function fails, it also sets the value of errno to indicate the error type: EBADF or EINVAL.

Example

#include <cstdio>

int main()
{
    char* cpFileName = "XoaX.txt";
    FILE* qpFile = fopen(cpFileName, "r" );

    fpos_t iPos = 0;
    // Get the initial position
    int iResult = fgetpos(qpFile, &iPos);
    if (iResult == 0) {
        printf("Position: %i \n", iPos);
    }
    // Read the first byte of the file to advance the file pointer.
    int iFirstChar = fgetc(qpFile);

    // Get the position after the read
    iResult = fgetpos(qpFile, &iPos);
    if (iResult == 0) {
        printf("Position: %i \n", iPos);
    }

    fclose(qpFile);

    return 0;
}

Input File:

fgetpos() Input File

Output:

fgetpos() Output