ftell() in C
0 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>Output: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; }
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).
Share:
Comments
Waiting for your comments