Tellp Function In C++

Introduction:

The tellp function returns the pointer's current "put" position in the stream when used with output streams. It has no parameters and returns a value of the member type pos_type , an integer data type that represents the put stream pointer's current position.

Syntax:

Example

pos_typetellp();

Return -

If successful, the current output position indication; otherwise, return -1 .

Example:1

Example

// utilizing the tellp() function, write some C++ code to find the position at a specific location.
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
fstream file;

    // reading and writing a file

file.open("file.txt", ios::out);
    file << "javaCppTutorial";

    // print the file's pointer position
cout<< "the current position of pointer is :"
<<file.tellp() <<endl;

    // close the open file
file.close();
}

Output:

Output

the current position of pointer is :10

Explanation:

In the code above, the tellp function returns the current position in the file to which icpp tutorials.

Example 2 -

Example

// code to add content using tellp() at a specific spot
#include <fstream>
using namespace std;

int main()
{
	long position;
	fstream file;

	// open the file in read and write mode
	file.open("myfile.txt");

	// Fill out the file with content
	file.write("this is an apple", 16);
	position = file.tellp();

	//set the pointer's position with seekp
	file.seekp(position - 7);
	file.write(" sam", 4);
	file.close();
}

Output:

Output

this is a sample

Explanation:

In this case, the tellp method returns the position of the pointer. After that, the pointer is shifted back from the current place by 7 positions using the seekp function , and the content is then inserted there.

Input Required

This code uses input(). Please provide values below: