In this guide, we will explore the variance between the tellg and tellp functions in C++. However, prior to delving into their distinctions, it is essential to comprehend the functionalities of tellg and tellp in C++.
What is the tellg function?
The tellg method provides the current position of the "get" pointer within the stream. This function does not require any arguments and outputs an integer value of the pos_type data type representing the current position of the get stream pointer.
The tellg function does not return information about the file size or the byte offset from the beginning. Instead, it returns a token value that can be used for future reference. There is no guarantee that this token can be converted to an integral type.
Syntax:
It has the following syntax:
pos_type tellg();
Return Type:
This function retrieves the current position of the gecpp tutorialer if the pointer is pointing to a valid location; otherwise, it returns "-1".
Example:
position = obj.tellg();
Whereas,
- index represents an integer variable.
- fileObj denotes an object related to file manipulation.
Example:
Let's consider a C++ code example to demonstrate the functionality of the tellg method.
#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 tellp function is employed with output streams to retrieve the current position where the pointer is set to "put" in the stream. This function does not require any arguments and provides an integer data type value of the member type pos_type, indicating the current location of the put stream pointer.
Syntax:
It has the following syntax:
pos_type tellp();
Return Type:
This function retrieves the current position of the gecpp tutorialer if the pointer is directed to a valid location; otherwise, it returns "-1".
Example:
position = obj.tellp();
Wherever used,
- position represents an integer variable.
- obj denotes an object used for file manipulation.
Example:
Let's consider a C++ code example to demonstrate the functionality of the tellp method.
#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 exist distinct variances between the tellg and tellp functions in C++. Key variations between the tellg and tellp functions include:
| 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. |