ftell() in C
×


ftell() in C

276

The ftell() function in C is used to determine the current position of the file pointer within a file.

It returns the current offset (in bytes) from the beginning of the file.

Syntax of ftell():

long ftell(FILE *stream);

stream: Pointer to the FILE object that identifies the file stream.

Return Value:

On success, ftell() returns the current position of the file pointer as a long integer.

On failure, it returns -1L.

Example:

#include<stdio.h>

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

    // Open a file named "example.txt" in read mode
    fp = fopen("example.txt", "r");

    // Check if the file opened successfully
    if (fp != NULL) {
        // Read and print the current position of the file pointer
        position = ftell(fp);
        printf("Current position: %ld\n", position);

        // Close the file
        fclose(fp);
    } else {
        printf("Error opening file.\n");
    }

    return 0;
}
Output:

Error opening file.

In this example:

We open a file named "example.txt" in read mode.

We use ftell(fp) to get the current position of the file pointer.

The current position is printed using printf().

Finally, we close the file with fclose(fp).



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments