C programming stdio.h function - long int ftell(FILE *stream)

www.igift‮aedi‬.com

In C programming, the ftell() function is defined in the stdio.h header file and is used to get the current position of the file pointer within a file. The function takes a single argument:

long int ftell(FILE *stream);

The argument stream is a pointer to a FILE object that represents the stream whose file pointer position is to be returned.

The ftell() function returns the current position of the file pointer as a long integer, representing the number of bytes from the beginning of the file. If an error occurs, the function returns -1.

Here's an example of how to use ftell() to get the current position of the file pointer within a file:

#include <stdio.h>

int main() {
    FILE *fp;
    long int position;

    fp = fopen("example.txt", "r");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    position = ftell(fp);
    printf("The current position of the file pointer is: %ld\n", position);

    fclose(fp);

    return 0;
}

In the above example, the ftell() function is used to get the current position of the file pointer within the fp stream. The position is stored in a long integer variable called position, and then printed to the console.