The Wayback Machine - https://web.archive.org/web/20240926041007/https://www.geeksforgeeks.org/tellg-function-c-example/
Open In App

tellg() function in C++ with example

Last Updated : 05 Jun, 2018
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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)



Similar Reads

Read a record from a File in C++ using seekg() and tellg()
Given that a binary file "student.data" is already loaded in the memory of the computer with the record of 100 students, the task is to read the Kth record and perform some operations.seekg() is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary position in a file. It is used in file handling t
2 min read
Difference between tellg and tellp in C++
In this article, we will discuss the functionality of basic_istream&lt;&gt;::tellg and basic_ostream&lt;&gt;::tellp and the difference between them. tellg(): The function is defined in the istream class, and used with input streams. It returns the position of the current character in the input stream. Syntax: pos_type tellg(); Return Type: If the p
2 min read
wcstok() function in C++ with example
The wcstok() function is defined in cwchar.h header file. The wcstok() function returns the next token in a null terminated wide string. The pointer delim points to the separator characters i.e. the delimiter. .Syntax: wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t ** ptr); Parameters: This method takes following parameters: str: It re
2 min read
wcsstr() function in C++ with example
The wcsstr() function is defined in cwchar.h header file. The wcsstr() function finds the first occurrence of a source in a destination string. Syntax: wchar_t* wcsstr(const wchar_t* dest, const wchar_t* src); Parameter: This method accepts the following parameters: src: It represents the pointer to the null terminating wide string to be search for
2 min read
wcsncpy() function in C++ with example
The wcsncpy() function is defined in cwchar.h header file. The wcsncpy() function copies a specified number of wide characters from source to destination. Syntax: wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n); Parameters: This method accepts the following three parameters: dest: specifies the pointer to the destination array. src: s
2 min read
Function Interposition in C with an example of user defined malloc()
Function interposition is the concept of replacing calls to functions in dynamic libraries with calls to user-defined wrappers. What are applications? We can count number of calls to function. Store caller's information and arguments passed to function to track usage. Detect memory leak, we can override malloc() and keep track of allocated spaces.
3 min read
vector::crend() &amp; vector::crbegin() with example
These functions return useful iterators to access vector elements in reverse order (from end to beginning) Using vector::crend() It's a public member function that returns a const_reverse_iterator pointing to the element preceding the first element. Return Value A const_reverse_iterator to the reverse end of the sequence. Syntax: const_reverse_iter
2 min read
asin() and atan() functions in C/C++ with Example
In C++, asin() and atan() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions. All the functions available in this library take double as an argument and return double as the result. asin() Method asin() function is used to find the arc sine of a number means give a sin
3 min read
forward_list::cend() in C++ STL with Example
forward_list::cend() is a function in C++ STL which returns a constant iterator pointing to the past-the-last element of the forward_list. The iterator returned by the function does not point to any element in the container, but to the position followed by the last element of the forward list container. The iterator returned by the function points
2 min read
Difference between Public and Private in C++ with Example
Public All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. Example: // C++ program to
3 min read
std::is_destructible in C++ with Example
The std::is_destructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_destructible template of C++ STL is used to check whether the T is destructible or not. A class is called destructible whose destructor is not deleted and potentially accessible in derived classes. It return the boolean value true if T is des
2 min read
std::is_nothrow_move_constructible in C++ with Example
The std::is_nothrow_move_constructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_nothrow_move_constructible template of C++ STL is used to check whether the given type T T is move constructibe or not and this is known for not to throw any exception. It return the boolean value true if T is a move constructib
2 min read
std::is_trivially_default_constructible in C++ with Example
The std::is_trivially_default_constructible template of C++ STL is present in the &lt;type_traits&gt; header file. The std::is_trivially_default_constructible template of C++ STL is used to check whether T is trivially default constructible type or not. It return the boolean value true if T is trivially default constructible type, otherwise return
3 min read
std::numeric_limits&lt;T&gt;::denorm_min() in C++ with Example
The std::numeric_limits&lt;T&gt;::denorm_min() function in C++ STL is present in the &lt;limits&gt; header file. This function is used to find the smallest non-zero denormalized value. Header File: #include&lt;limits&gt; Template Class: static T denorm_min() throw(); static constexpr T denorm_min() noexcept; Syntax: std::numeric_limits&lt;T&gt;::de
1 min read
std::numeric_limits<T>::digits in C++ with Example
The std::numeric_limits::digits function in C++ STL is present in the &lt;limits&gt; header file. The std::numeric_limits::digits function is used to find the number of radix digits that the data type can represent without loss of precision. Header File: #include&lt;limits&gt; Template: static const int digits; static constexpr int digits; Syntax:
1 min read
std::remove_cv in C++ with Example
The std::remove_cv template of C++ STL is present in the &lt;type_traits&gt; header file. The std::remove_cv template of C++ STL is used to get the type T without const and volatile qualification. It return the boolean value true if T is without const and volatile qualified, otherwise return false. Header File: #include&lt;type_traits&gt; Template:
1 min read
std::underlying_type in C++ with Example
The std::underlying_type template of C++ STL is present in the &lt;type_traits&gt; header file. The std::underlying_type template of C++ STL is used to get the underlying type of enum type T. Header File: #include&lt;type_traits&gt; Template Class: template &lt;class T&gt; struct underlying_type; Syntax: std::underlying_type&lt;class T&gt;::value P
1 min read
Life cycle of Objects in C++ with Example
In Object Oriented Programming, Objects are the instances of a class which has its own state(variables) and behavior(methods). Every class has two special methods related with creation and destruction of object of a class- constructors and destructors. C++ Object Life Cycle: There are various steps that need to be followed to complete the life cycl
6 min read
BigInt (BIG INTEGERS) in C++ with Example
In C/C++ the number of digits a long long int can have is a maximum of 20. And the question is to store the 22 digit number which is not easy to store in any kind of primitive type. So to deal with this type of problem let's design a new data type which is going to be called BigInt In this article, a few basic operations are being implemented on th
15+ min read
Buffer Overflow Attack with Example
A buffer is a temporary area for data storage. When more data (than was originally allocated to be stored) gets placed by a program or system process, the extra data overflows. It causes some of that data to leak out into other buffers, which can corrupt or overwrite whatever data they were holding.In a buffer-overflow attack, the extra data someti
3 min read
fseek() in C/C++ with example
fseek() is used to move the file pointer associated with a given file to a specific position. Syntax of fseek() The fseek() syntax is: int fseek(FILE *pointer, long int offset, int position);Parameterspointer: It is the pointer to a FILE object that identifies the stream.offset: It is the number of bytes to offset from the positionposition: It is t
2 min read
Writing First C++ Program - Hello World Example
C++ is a widely used Object Oriented Programming language and is relatively easy to understand. The "Hello World" program is the first step towards learning any programming language and is also one of the most straightforward programs you will learn. The Hello World Program in C++ is the basic program that is used to demonstrate how the coding proc
4 min read
std::search_n with example in C++
Prerequisite: std::search std::search_n is an STL algorithm defined inside the header file , which is used to search whether a given element satisfies a predicate (equality if no such predicate is defined ) a given no. of times consecutively with the container elements. It searches the range [first, last) for a sequence of count elements, each comp
4 min read
tellp() in file handling with c++ with example
The tellp() function is used with output streams, and returns the current "put" position of the pointer in the stream. It has no parameters and return a value of the member type pos_type, which is an integer data type representing the current position of the put stream pointer.Syntax: pos_type tellp(); Return - Current output position indicator on
2 min read
Difference between Private and Protected in C++ with Example
Protected Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.Example: [GFGTABS] CPP // C++ program to demonstrate // protected access modifier #include &amp;lt;
3 min read
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Pre-requisite: Function Overloading in C++ Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. Two or more functions are said to be overloaded if they differ in any of th
9 min read
How to call some function before main() function in C++?
Since it is known that main() method is the entry point of the program. Hence it is the first method that will get executed by the compiler. But this article explains how to call some function before the main() method gets executed in C++. How to call some function before main() function? To call some function before main() method in C++, Create a
3 min read
Difference between user defined function and library function in C/C++
Library Functions These functions are the built-in functions i.e., they are predefined in the library of the C. These are used to perform the most common operations like calculations, updation, etc. Some of the library functions are printf, scanf, sqrt, etc. To use these functions in the program the user has to use a header file associated with the
3 min read
Difference between virtual function and inline function in C++
Virtual function: Virtual function is a member function which is declared within a base class and is redefined by a derived class. Inline function: Inline function is a normal function which is defined by the keyword inline, it is a short function which is expanded by the compiler and its arguments are evaluated only once. The syntax of defining th
2 min read
Difference Between Friend Function and Virtual Function in C++
A friend class can access private and protected members of other classes in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other classes. Just likely, a friend function is a function that is declared outside the scope of a class. This function can be invoked like a normal function and
3 min read
Practice Tags :