C Standard Libraries C++

fseek()

Declaration

int fseek(FILE* qpStream, long lOffset, int iStart);

Description

This function sets the position of the internal file pointer for the stream "qpStream." The file pointer gets set to the postion "iStart" plus the value of "lOffset." The Value "iStart" must be one other following constant value: "SEEK_SET," "SEEK_CUR," or "SEEK_END." These value correspond to the beginning, current pointer location, and end positions in the file. If the function is successful, it return 0. Otherwise, it returns a nonzero value to indicate an error.

Input File

fseek() Input File

Example

#include <cstdio>

int main()
{
    char* cpFileName = "XoaX.txt";
    // Open an ASCII text file for reading.
    FILE* qpFile = fopen(cpFileName, "r" );

    // Check that the file was opened successfully.
    if (!qpFile) {
        printf("Could not open %s for reading \n",  cpFileName);
        return 1;
    }

    // Set the pointer to position 2 in the file.
    int iRet = fseek(qpFile, 2, SEEK_SET);
    if (iRet == 0) {
        // Read in 'a' at two places from the beginning
        int iRead = fgetc(qpFile);
        printf("Read in: %c\n", (char)iRead);
    }
    fclose(qpFile);

    return 0;
}

Output

fseek() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.