Home  >  Article  >  Backend Development  >  In C language, the ftell() function is used to obtain the current position of the file pointer

In C language, the ftell() function is used to obtain the current position of the file pointer

王林
王林forward
2023-09-08 20:53:02786browse

In C language, the ftell() function is used to obtain the current position of the file pointer

In C language, ftell() returns the current file position of the specified stream relative to the beginning of the file. This function is used to get the total size of the file after moving the file pointer to the end of the file. It returns the current position as a long and the file can contain more than 32767 bytes of data.

This is the syntax of ftell() in C language,

long int ftell(FILE *stream)

This is the parameter used in ftell(),

  • stream - This is a pointer to a FILE object that identifies

This is an example of ftell() in C.

Suppose we have a file "one.txt" with the following content.

This is demo text!
This is demo text!
This is demo text!

Now, let’s look at an example.

Example

#include <stdio.h>
#include<conio.h>
void main () {
   FILE *f;
   int len;
   f = fopen("one.txt", "r");
   if(f == NULL) {
      perror(&ldquo;Error opening file&rdquo;);
      return(-1);
   }
   fseek(f, 0, SEEK_END);
   len = ftell(f);
   fclose(f);
   printf("Size of file: %d bytes", len);
   getch();
}

Output

Size of file: 78 bytes

The above is the detailed content of In C language, the ftell() function is used to obtain the current position of the file pointer. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete