Open In App
Related Articles

tellg() function in C++ with example

Improve Article
Improve
Save Article
Save
Like Article
Like

The tellg() function is used with input streams, and returns the current “get” position of the pointer in the stream. It has no parameters and returns a value of the member type pos_type, which is an integer data type representing the current position of the get stream pointer.

Syntax:-

pos_type tellg(); 

Returns: The current position of the get pointer on success, pos_type(-1) on failure.

Example 1




// C++ program to demonstrate 
// example of tellg() function.
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    string str = "geeksforgeeks";
    istringstream in(str);
  
    string word;
    in >> word;
    cout << "After reading the word \"" << word
        << "\" tellg() returns " << in.tellg() << '\n';
}


Output:

After reading the word "geeksforgeeks" tellg() returns -1

Example 2 :




// C++ program to demonstrate 
// example of tellg() function.
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    string str = "Hello, GFG";
    istringstream in(str);
  
    string word;    
    in >> word;
      
    cout << "After reading the word \"" << word
        << "\" tellg() returns " << in.tellg() << '\n';
}


Output:

After reading the word "Hello," tellg() returns 6

Properties:-
tellg() does not report the size of the file, nor the offset from the beginning in bytes. It reports a token value which can later be used to seek to the same place, and nothing more. (It’s not even guaranteed that you can convert the type to an integral type)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Jun, 2018
Like Article
Save Article
Previous
Next
Similar Reads