cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : istream : tellg
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
istream
· istream::istream
· istream::~istream
member classes:
· istream::sentry
member functions:
· istream::gcount
· istream::get
· istream::getline
· istream::ignore
· istream::operator>>
· istream::peek
· istream::putback
· istream::read
· istream::readsome
· istream::seekg
· istream::sync
· istream::tellg
· istream::unget

-

istream::tellg public member function
streampos tellg ( );

Get position of the get pointer.

Returns the absolute position of the get pointer.

The get pointer determines the next location in the input sequence to be read by the next input operation.

Parameters

none

Return Value

An integral value of type streampos with the number of characters between the beginning of the input sequence and the current position.

Failure is indicated by returning a value of -1.

Example

// read a file into memory
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  char * buffer;

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);
  length = is.tellg();
  is.seekg (0, ios::beg);

  // allocate memory:
  buffer = new char [length];

  // read data as a block:
  is.read (buffer,length);

  is.close();

  cout.write (buffer,length);

  return 0;
}

In this example, tellg is used to get the offset of the last character in the stream, therefore determining the size of the file.

Basic template member declaration

(basic_istream<charT,traits>)
typedef traits::pos_type pos_type;
pos_type tellg ();

See also

istream::seekg Set position of the get pointer (public member function)
ostream::tellp Get position of put pointer (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us