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:
- obj.open (“test.txt”, ios::out);
Here we are opening a text file called test with main purpose to write. - 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. - int pos;
Here we defined a variable pos of type int to store the current put pointer position. - pos = obj.tellp();
Using the tellp() syntax, we stored the current put pointer position in pos. - cout<<pos;
Here we are printing the value of pos which has the put pointer’s current position. - 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:
- 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. - 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. 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.- 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++
thers a mistake in output i think
1.H
2.e
3.l
4.l
5.o
7.W
8.o
9.r
10.l
11.d
output is 10 because they forget 6
umm sorry i just noticed u didn’t counted space so its fine sorry