tellp() and tellg() in File Handling in C++

Hey guys, In this tutorial, you will understand what tellp() and tellg() functions are and how they work in C++.

So as I explained in my last tutorial (Tutorial: File Handling in C++),
We have two functions, tellp() and tellg(), which gets imported along with the other basic file handling operations in “fstream”.

Let us see them one by one.

tellp() in C++

tellp() tells the current pointer position in the text file.

Say we have entered 20 characters in a text file. Your current pointer is at 19. Since pointer value starts from 0 therefore 20-1=19.
So if we use tellp() in this text file we would get an output which is 19 of type int.

Using tellp() we can edit the text file from the last position in the text file or wherever the current pointer is.

NOTE:
tellp = tell put pointer.
So it tells where your put pointer is.
I.E. for the write operation.

Syntax: tellp()

variable = <file_handling_object>.tellp();

Your variable should be of type int.

Example:
pos = obj.tellp();

Where,
pos is a variable of type int.
obj is a file handling object.

Code Example: tellp() in C++

Take a look at the following code:

#include <iostream>
#include <fstream>
using namespace std;
int main () 
{
  fstream obj;
  obj.open ("test.txt", ios::out);
  obj<<"Hello World";
  int pos;
  pos = obj.tellp();
  cout<<pos;
  obj.close();
}

Let us see it line by line:

  1. obj.open (“test.txt”, ios::out);
    Here we are opening a text file called test with main purpose to write.
  2. obj<<“Hello World”;
    Here we are writing Hello World into the text file.
    Notice there are 12 characters from H to d when we count from 1.
  3. int pos;
    Here we defined a variable pos of type int to store the current put pointer position.
  4. pos = obj.tellp();
    Using the tellp() syntax, we stored the current put pointer position in pos.
  5. cout<<pos;
    Here we are printing the value of pos which has the put pointer’s current position.
  6. obj.close();
    And finally, here we are closing the text file we opened.

Hence our output would be:

11

11 since we start counting from 0.

tellg() in C++

tellg() tells the current pointer position in the text file.
But instead of put pointer, it tells the get pointer’s location

Say we have entered 20 characters in a text file, and you want to read it.
But along with reading you also want to know the position of the last position in the text file.
That is where tellg() comes into play.
It would give you the output 19 since counting starts from 0.

NOTE:
tellg = tell get pointer.
So it tells where your get pointer is.
I.E. for the read operation.

Syntax: tellg()

variable = <file_handling_object>.tellg();

Your variable should be of type int.

Example:
pos = obj.tellg();

Where,
pos is a variable of type int.
obj is a file handling object.

Code Example: tellg() in C++

Take a look at the following code:

#include <iostream>
#include <fstream>
using namespace std;
int main () 
{
  fstream obj;
  obj.open ("test.txt", ios::in);
  char ch;
  int pos;
  while(!obj.eof())
  {
    obj>>ch;
    pos = obj.tellg();
    cout<<pos<<"."<<ch<<"\n";
  }
  obj.close();
}

Let us see it line by line:

  1. obj.open (“test.txt”, ios::in);
    Here we are opening a text file called test with main purpose to read.
    I already have “Hello World” written in the test file.
  2. char ch;
    int pos;
    We are defining 2 variables: “ch” of type char to read each character, and “pos” of type into to get the position of the get pointer.
  3. while(!obj.eof())
    
    {
    
    obj>>ch;
    
    pos = obj.tellg();
    
    cout<<pos<<"."<<ch<<"\n";
    
    }

    So this is the basic code to read a file.
    Here we are using pos=obj.tellg() to get the pointer value of character being read.
    Finally, we are printing the position and the character each on a separate line.

  4. obj.close();
    And finally, we are closing the text file we opened.

Hence the output would be:

1.H    
2.e   
3.l 
4.l  
5.o 
7.W  
8.o  
9.r 
10.l 
11.d

And hence we have covered tellp() and tellg() in File Handling in C++

 

Leave a Reply

Your email address will not be published. Required fields are marked *