Open In App

fseek() in C/C++ with example

fseek() is used to move the file pointer associated with a given file to a specific position.

Syntax of fseek()

The fseek() syntax is:



int fseek(FILE *pointer, long int offset, int position);

Parameters

Return Value

Example

The below C Program demonstrates the use of fseek() function.




// C Program to demonstrate the use of fseek()
#include <stdio.h>
 
int main()
{
    FILE* fp;
    fp = fopen("test.txt", "r");
 
    // Moving pointer to end
    fseek(fp, 0, SEEK_END);
 
    // Printing position of pointer
    printf("%ld", ftell(fp));
 
    return 0;
}

Suppose the file test.txt contains the following data:



"Someone over there is calling you.
we are going for work.
take care of yourself."

Output

81

Explanation

When we implement fseek(), we move the pointer by 0 distance with respect to the end of the file i.e. pointer now points to the end of the file. Therefore the output is 81.

Related Articles:

Article Tags :