EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

C ftell()

Home » Software Development » Software Development Tutorials » C Programming Tutorial » C ftell()

C ftell()

Introduction to C ftell()

The C ftell() function is used to return the current position of the specified file stream. The ftell() function is a built-in function in c. Some times in program while we reading or writing the data from or to the file we need to get the current position of the file to read data from a specific location or to write the data to a specific location, so to get the current location of the file pointer we can use ftell() function and the later to change or move the pointer location we can use the fseek() function(), which is also a built-in function. The ftell() function accepts file pointer which points to the specific file, so this function returns the current position of that specific file and this function also can be used to return the size of the file by moving the pointer to the end of the file with the help of SEEK_END constant value.

The Syntax of the ftell() function in C

Following is the syntax to call the ftell() function in c –

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

long int ftell(FILE *fstream);

Parameters –

*fstream - *fstream parameter specifies the FILE type pointer which points to specific FILE object.

Return value –

The return value of the function as is int, it returns the current location of the file pointer pointing, otherwise returns -1L if any error occurs.

Working and Examples of the ftell() function in C

Next, we write the C code to understand the ftell() function working more clearly with the following example where we use ftell() function to get the current location of the file pointed by the pointer, as below –

Example #1

Code:

#include<stdio.h>
void main()
{
char fdata[50];
// Open f1.txt file in read mode
// fstream is the FILE type pointer which will point to the FILE object or data.txt
FILE *fstream = fopen("data.txt","r");
// get the location of pointer
printf("The current location of th pointer before reading from the file is : %ld\n", ftell(fstream));
// store the read from the file to fdata array
fscanf(fstream,"%s",fdata);
printf("The current data read from the file is : %s\n", fdata);
printf("The current location of th pointer after reading from the file is : %ld\n", ftell(fstream));
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,570 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

C ftell() output 1

As in the above code, the file “data.txt” is opened and the fstream is a FILE type pointer which is pointing to this file, if any operation needs to perform-like read, write, append, etc, we can perform with the help of this FILE pointer(fstream). When the new file is open the file pointer always points to the starting position of the file that is 0 in the file. Farther in the code the ftell() function is used before and after reading some data from the file. So before reading the data the ftell() return the pointer location is 0, after reading data “This” which is of four lengths the ftell() return the pointer location is 4, which are correct.

Next, we write the C code to understand the ftell() function working where we use ftell() function to get the total length of the file by using the file pointer, as below –

Example #2

Code:

#include<stdio.h>
void main()
{
char fdata[50];
int length;
// Open f1.txt file in read mode
// fstream is the FILE type pointer which will point to the FILE object or data.txt
// data.txt file contain "This is the file data." in file.
FILE *fstream = fopen("data.txt","r");
// get the location of pointer
printf("The current location of th pointer before seek is : %ld\n", ftell(fstream));
// fseek() function move the file pointer
fseek(fstream, 0, SEEK_END);
length = ftell(fstream);
printf("The total length the file is : %ld\n", length);
printf("The current location of th pointer after seek is : %ld\n", ftell(fstream));
}

Output:

C ftell() output 2

As in the above code, the file “data.txt” is open which stores the data “This is the file data.” of length 22 and the fstream is a FILE type pointer which is pointing to this file. Farther in the code the fseek() function is used to move the pointer to the end of the file with the help of SEEK_END constant value and then after moved with the help of ftell() function return the pointer location which is 22 that is the last location or index pointing by the point and that is what the length of the file.

Next, we write the C code to understand the ftell() function working where we use ftell() function to get the location of the file which does not exist or cannot open by using the file pointer, as below –

Example #3

Code:

#include<stdio.h>
void main()
{
int i;
// Open f1.txt file in read mode
// data1.txt file does not exits.
FILE *fstream = fopen( "data1.txt","r" );
i = ftell(fstream);
if(i == -1L)
{
printf( "A file error has occurred!!\n" );
}
// get the location of pointer
printf( "The current location of the pointer is : %ld\n", ftell(fstream) );
}

Output:

output 3

As in the above code, the file “data1.txt” is trying to open but that file does not exist. The fstream FILE type pointer is trying to point to this file as the file does not exist the fopen() function return 0 and so the ftell(fstream) function return -1L, as because the error occurs to open the file.

Conclusion

The ftell() function is a built-in function in C, which is used to return the current position of the file stream. The ftell() function accepts one parameter of File type pointer which points to the file.

Recommended Articles

This is a guide to C ftell(). Here we discuss an introduction to ftell() with the working of this function and respective examples for better understanding. You may also look at the following articles to learn more –

  1. strcat() in C
  2. Recursive Function in C
  3. Square Root in C
  4. ceil function in C++

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More