Namespaces
Variants
Views
Actions

std::basic_istream::tellg

From cppreference.com
< cpp‎ | io‎ | basic istream
 
 
Input/output library
I/O manipulators
C-style I/O
Buffers
(deprecated in C++98)
Streams
Abstractions
File I/O
String I/O
Array I/O
(deprecated in C++98)
(deprecated in C++98)
(deprecated in C++98)
Synchronized Output
Types
Error category interface
(C++11)
 
 
pos_type tellg();

Returns input position indicator of the current associated streambuf object.

Behaves as UnformattedInputFunction, except that gcount() is not affected. After constructing and checking the sentry object, if fail() == true, returns pos_type(-1). Otherwise, returns rdbuf()->pubseekoff(0, std::ios_base::cur, std::ios_base::in).

Contents

[edit] Parameters

(none)

[edit] Return value

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

[edit] Exceptions

failure if an error occurred (the error state flag is not goodbit) and exceptions() is set to throw for that state.

If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown.

[edit] Example

#include <iostream>
#include <string>
#include <sstream>
 
int main()
{
    std::string str = "Hello, world";
    std::istringstream in(str);
    std::string word;
    in >> word;
    std::cout << "After reading the word \"" << word
              << "\" tellg() returns " << in.tellg() << '\n';
}

Output:

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

[edit] See also

[virtual]
repositions the file position, using relative addressing
(virtual protected member function of std::basic_filebuf) [edit]
[virtual]
repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
(virtual protected member function of std::basic_stringbuf) [edit]
[virtual]
repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
(virtual protected member function of std::strstreambuf) [edit]
sets the input position indicator
(public member function) [edit]
returns the output position indicator
(public member function of std::basic_ostream) [edit]
sets the output position indicator
(public member function of std::basic_ostream) [edit]