In C Language, "ftell()" is a standard library function used to determine the current position of the file pointer within a file.
It returns the current file position indicator as a long integer, representing the number of bytes from the beginning of the file.
long ftell(FILE *stream);
"stream": Pointer to a "FILE" object that identifies the file.
The "ftell()" function returns the current position of the file pointer as a long value. If an error occurs, "ftell()" returns -1L, and errno is set to indicate the error.
"ftell()" is used to determine the current position of the file pointer within a file.
#include <stdio.h> int main() { FILE *file; long position; // Open the file in read mode file = fopen("example.txt", "r"); // Check if file opened successfully if (file == NULL) { perror("Error opening file"); return 1; } // Get the current position of the file pointer position = ftell(file); // Print the current position printf("Current position of the file pointer: %ld\n", position); // Close the file fclose(file); return 0; }
#include <stdio.h> int main() { FILE *file; long position; // Open the file in read mode file = fopen("example.txt", "r"); // Check if file opened successfully if (file == NULL) { perror("Error opening file"); return 1; } // Move the file position indicator to the middle of the file fseek(file, 0, SEEK_END); // Move to end of file fseek(file, -ftell(file) / 2, SEEK_CUR); // Move back half the distance // Get the current position of the file pointer position = ftell(file); // Print the current position printf("Current position of the file pointer: %ld\n", position); // Close the file fclose(file); return 0; }
"ftell()" is used after moving the file position indicator within a file to determine the current position.
"ftell()" is used to get the current position of the file pointer within the file "example.txt".
The position is then printed to the console. Finally, the file is closed using "fclose()".