Home > Backend Development > C++ > 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

王林
Release: 2023-09-08 20:53:02
forward
939 people have browsed it

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)
Copy after login

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!
Copy after login

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();
}
Copy after login

Output

Size of file: 78 bytes
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template