fgetpos() function in C language with Example

Here, we are going to learn about the fgetpos() function of library header stdio.h in C language with its syntax, example.
Submitted by Souvik Saha, on January 07, 2019

fgetpos() function in C


    int fgetpos(FILE* filename, fpos_t *position);


    FILE* filename, fpos_t *position,

Return type: int (the current position of the file indicator pointer)

Use of function:

In file handling, through the fgetpos() function we get the current position of the input file stream indicator. Whenever we need the file indicator position in the file, we need to use the function fgetpos(). The prototype of the fgetpos() function is: int fgetpos(FILE* filename, fpos_t *position);

Here the data type of the position variable must be fpos_t type.

fgetpos() example in C

#include <stdio.h>
#include <stdlib.h>

int main()
    FILE* f;
    char ch[100];

    //Initialize the position variable
    fpos_t pos;

    //Create the file for write operation
    f = fopen("includehelp.txt", "w+");

    //Store the value of the function point indicator
    fgetpos(f, &pos);

    printf("Enter five strings\n");
    for (int i = 0; i < 4; i++) {
        //take the strings from the users
        scanf("%[^\n]", &ch);
        //write back to the file
        fputs(ch, f);
        //every time take a new line for the new entry string
        //except for last entry.Otherwise print the last line twice
        fputs("\n", f);

        //clear the stdin stream buffer
        //if we don't write this then after taking string
        //%[^\n] is waiting for the '\n' or white space

    //take the strings from the users
    scanf("%[^\n]", &ch);
    fputs(ch, f);
    //set the indicator position to the initial position of the file
    fsetpos(f, &pos);

    printf("File content is--\n");
    printf("\n...............print the strings..............\n\n");
    while (!feof(f)) {
        //takes the first 100 character in the character array
        fgets(ch, 100, f);
        //and print the strings
        printf("%s", ch);

    //close the file

    return 0;


fgetpos example in c

C stdio.h Library Functions Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.