ftell() in C

The ftell() function in C returns the current position of the file pointer in a file stream. It is a part of the standard input/output library and is used to determine the file pointer's current offset from the beginning of the file.

Syntax

                        
long ftell(FILE *stream);
                        
                    

Parameters

The ftell() function returns the current file position as a long integer. If an error occurs, it returns -1L.

Example: Using ftell()

                        
#include <stdio.h>
int main() 
{
    FILE *file = fopen("example.txt", "w+");
    if (file == NULL) {
        printf("Error opening file\n");
        return 1;
    }

    // Write data to the file
    fputs("Hello, World!", file);

    // Get the current position of the file pointer
    long position = ftell(file);
    if (position != -1L) {
        printf("Current file position: %ld\n", position);
    } else {
        printf("Error determining file position \n");
    }

        
    fclose(file);
    return 0;
}
                        
                    

Output:

Current file position: 13