tellp() in file handling with c++ with example
Last Updated :
12 Jul, 2021
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 success otherwise return -1.
Example 1 –
Output –
the current position of pointer is :-1
In the above code the tellp() returns the current position to which it point in a file.
Example 2 –
CPP
#include <fstream>
using namespace std;
int main()
{
long position;
fstream file;
file.open( "myfile.txt" );
file.write( "this is an apple" , 16);
position = file.tellp();
file.seekp(position - 7);
file.write( " sam" , 4);
file.close();
}
|
Output –
this is a sample
Here tellp() function returns the position of pointer then using seekp() function the pointer is shift back from n position here it shift 7 position back and then insert the content at that position .
Share your thoughts in the comments
Please Login to comment...