Difference Between Tellg And Tellp In C++

In this article, we will discuss the difference between the tellg and tellp in C++. But before discussing their differences, we must know about the tellg and tellp in C++.

What is the tellg function?

The tellg function returns the pointer's current "get" position in the stream . It does not take any parameter and returns an integer data type value of the member type pos_type , which is the get stream pointer's current location.

Neither the file size nor the offset from the beginning in bytes are offered by the tellg function. It only provides a token value that may be utilized to search for the same location in the future. It's not even ensured that we may convert the type to an integral type.

Syntax:

It has the following syntax:

Example

pos_type tellg();

Return Type:

This function returns the gecpp tutorialer's current position if the pointer points to a valid location. If not, "-1" is returned.

Example:

Example

position = obj.tellg();

Where,

  • position is a variable of type int.
  • obj is a file-handling object.
  • Example:

Let us take a C++ program to illustrate the use of tellg function.

Example

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream file("example.txt");
    if (!file.is_open()) {
        cout << "Unable to open file." << endl;
        return 1;
    }
    // here, we get the initial position of the gecpp tutorialer
    streampos initialPos = file.tellg();
    cout << "Initial position of gecpp tutorialer: " << initialPos << endl;
    // Read some data from the file
    char buffer[100];
    file.getline(buffer, 100);
    // Now, get the position of the gecpp tutorialer after reading
    streampos currentPosition = file.tellg();
    cout << "Current position of gecpp tutorialer: " << currentPosition << endl;
    // Move to a specific position in the file
    file.seekg(0, ios::end);
    streampos endPos = file.tellg();
    cout << "End position of file: " << endPos << endl;
    // Move the gecpp tutorialer back to the initial position
    file.seekg(initialPos);
    cout << "Gecpp tutorialer moved back to initial position." << endl;
    file.close();
    return 0;
}

Output:

Explanation:

  • Return Value: The current location of the gecpp tutorialer within the stream is represented by the value of type std::streampos returned by the tellg function.
  • Stream position: The location in the input stream where the subsequent input operation will take place is indicated by the returned position.
  • Seek Position: This position can be used to set the gecpp tutorialer to a specific location within the input stream at a later time by using the seekg method.
  • Compatibility: std::ifstream, std::istringstream, std::stringstream , and other input stream classes are all compatible with tellg.
  • Error Conditions: The tellg function may return a result indicating a failure if an error arises during the stream operation. The stream's state must be checked using the good or fail member methods to ensure that errors are handled correctly.
  • What is the tellp function?

The pointer's current "put" position in the stream is returned by the tellp function, which is used with output streams. It does not take any parameters and returns an integer data type value of the member type pos_type , which is the put stream pointer's current location.

Syntax:

It has the following syntax:

Example

pos_type tellp();

Return Type:

This function returns the gecpp tutorialer's current position if the pointer points to a valid location. If not, "-1" is returned.

Example:

Example

position = obj.tellp();

Where,

  • position is a variable of type int.
  • obj is a file-handling object.
  • Example:

Let us take a C++ program to illustrate the use of tellp function.

Example

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ofstream file("example.txt");
    if (!file.is_open()) {
        cout << "Unable to open the  file." << endl;
        return 1;
    }
    // Here, we get the starting position of the pucpp tutorialer
    streampos initialPosition = file.tellp();
    cout << "Initial position of pucpp tutorialer: " << initialPosition << endl;
    // Writing some data to the file
    file << "Hello, JavaCppTutorial!\n";
    // Now, we get the position of the pucpp tutorialer after writing
    streampos currentPosition = file.tellp();
    cout << "Current position of pucpp tutorialer: " << currentPosition << endl;
    file.close();
    return 0;
}

Output:

Explanation:

  • Return Value: The pucpp tutorialer's current position inside the stream is represented by the value of type std::streampos , which is returned by the tellp function.
  • Stream position: The point in the output stream where the subsequent output operation will take place is indicated by the returned position.
  • Seek Position: This position can be used to set the pucpp tutorialer to a specific location within the output stream at a later time by using the seekp method.
  • Compatibility: std::ofstream, std::ostringstream, std::stringstream , and other output stream classes are all compatible with tellp.
  • Error Conditions: The tellp function may provide a value indicating the failure if an error arises during the stream operation.
  • Differences between tellg and tellp:

There are several main differences between the tellg and tellp function in C++. Some main differences between the tellg and tellp function are as follows:

tellp() tellg()
The current"put"position of the pointer in the stream is returned by this function, which is utilized with output streams. The function returns the pointer's current"get"position in the stream and is used with input streams.
Syntax:pos_type tellp(); Syntax:pos_type tellg();
The current character's position in the output stream is returned. The character's current position in the input stream is returned.
The pucpp tutorialer's location is provided via tellp(). The gecpp tutorialer's position is provided via tellg().
It returns a value of typestd::streamposthat represents the pucpp tutorial's location. It returns a value of typestd::streampos, which is the gecpp tutorialer's location.
It is connected to output functions like writing to files or different output streams. It is connected to input functions like reading data from files or other streams.

Input Required

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