Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is ftell() Function in C?
3.
Example of ftell()
3.1.
C
4.
Frequently Asked Questions
4.1.
What is file handling and its types?
4.2.
What is the importance of file handling in C?
4.3.
What are file modes in C?
4.4.
How do I use ftell() function in C++?
5.
Conclusion
Last Updated: Jul 4, 2024
Easy

C ftell() function

Author Abhay Trivedi
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The ftell() function in the C programming language returns the current file position of the specified stream. If some error occurs, -1L is returned, and the global variable "errno" is set to a positive value.

What is ftell() Function in C?

he ftell() function in C is used to obtain the current file position of a stream. It returns a long integer value representing the current position in the file, measured in bytes from the beginning. This function is particularly useful for determining the position to later return to a specific location in a file. If an error occurs, ftell() returns -1L, and the errno variable is set to indicate the error.

syntax of ftell()

long ftell(FILE *pointer)

 

Parameters:

stream

This is the pointer to a FILE object that determines the stream.

Consider the following example for better understanding.

Must Read Decision Making in C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of ftell()

The file taken contains the following data: 

“Yesterday was history, tomorrow is a mystery, but today is a gift, which is why we call it the present.” (without the quotes)

  • C

C

#include <stdio.h>

int main()
{
   /* Opening file in the read mode */
   FILE *fp = fopen("file.txt", "r");

   /* Reading the first string */
   char string[25];
   fscanf(fp,"%s", string);

   printf("%ld \n", ftell(fp));
  
   /* Using SEEK_END constant to move the file pointer to the end of file. */
   fseek(fp, 0, SEEK_END);
  
   int length = ftell(fp);
  
   printf("Size of file: %d bytes", length);
 
   return 0;
}

 

Output:

9
Size of file: 103 bytes

 

Explanation:

When the fscanf() statement is executed word “Yesterday” is stored in the string, and the pointer is moved beyond “Yesterday”. Therefore ftell(fp) returns 9 as the length of “someone” is 8.

We can also use the ftell() function to get the file size after moving the file pointer to the end of the file.

You can also read about dynamic array in c and Short int in C Programming

Frequently Asked Questions

What is file handling and its types?

File Handling is the process of storing data in a file with a program. In the C programming language, the programs keep results and other program data to a file using file handling. We can also extract/fetch data from a file to work with it in the program.

What is the importance of file handling in C?

Reusability: When a program is terminated, the data will get lost. You can store your data in a file; even if the program terminates, you will not lose your data.

Portability: You can quickly move the file's data from one computer to another without any changes.

What are file modes in C?

There are many modes for opening a file:

  • r - open a file in reading mode.
  • w - opens or creates a text file in write mode.
  • a - opens a file in append mode.
  • r+ - opens a file in both read and write mode.
  • a+ - opens a file in both read and write mode.
  • w+ - opens a file in both read and write mode.

How do I use ftell() function in C++?

The ftell() function in C++ takes a file stream as its argument and returns the current value of the file pointer indicator for the given stream as a long int type. It is defined in <cstdio> header file. You can use it like:

long ftell(FILE* stream);

Conclusion

This article gives information about the ftell() function in the C programming language. We see how one can utilize the functionality the ftell() function provides and use it in their programs.

Also read,

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations. Also, Check our CN Library for free online coding resources.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Previous article
Rewind() Function in C
Next article
fseek() vs rewind() in C
Live masterclass