C <stdio.h> - ftell() Function
The C <stdio.h> ftell() function returns the current value of the file position indicator for the file stream stream.
For binary streams, it is the number of bytes from the beginning of the file.
If the stream is open in text mode, the value returned by this function is unspecified and is only meaningful as the input to fseek() function.
Syntax
long int ftell ( FILE * stream );
Parameters
stream |
Specify a pointer to a FILE object that identifies the stream. |
Return Value
On success, returns current value of the file position indicator. On failure, returns -1L and sets errno to a system-specific positive value.
Example:
Lets assume that we have a file called test.txt. This file contains following content:
This is a test file. It contains dummy content.
In the example below, file is opened using fopen() function. Then, by using fseek() and ftell() functions, size of the file is determined.
#include <stdio.h> int main (){ //open the file in read mode FILE *pFile = fopen("test.txt", "r"); //go to the end of file fseek(pFile, 0, SEEK_END); //get the current value of the //file position indicator long int size = ftell(pFile); //close the file fclose(pFile); //display the result printf("Size of the file: %ld bytes\n", size); return 0; }
The output of the above code will be:
Size of the file: 48 bytes
❮ C <stdio.h> Library