Tellg in C++
When used with input streams, the tellg() function returns the pointer's current "get" position in the stream. It takes no parameters and returns an integer data type value of the member type pos_type, which is the get stream pointer's current position.
In actuality, the purpose of tellg() is to return a pos_type descriptor, which seekg() can reuse, rather than the offset of a byte in a stream. If the file is binary, it will match the byte offset; however, in a text stream, this is not guaranteed.
Example 1:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string s = "tutorials and examples";
istringstream in(s);
string phrase;
in >> phrase;
cout << "After it reads the word \"" << phrase
<< "\" tellg() returns " << in.tellg() << '\n';
return 0;
}
Following its reading of the word "tutorials" from the istringstream, this code will output the position of the next character to be extracted. Stream positions are typical of the integer type streampos, and the output will depend on the size of that data type.
Output
![Tellg in C++](https://static.tutorialandexample.com/cpp/tellg-in-cpp1.png)
Example 2:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string s = "Hello, t&e";
istringstream in(s);
string phrase;
in >> phrase;
cout << "After it reads the word \"" << phrase
<< "\" tellg() returns " << in.tellg() << '\n';
return 0;
}
After reading the first word from the string "Hello, t&e," the code uses an istringstream to read the next character, which is 6, and prints the position of that character (the tellg() value).
Output
![Tellg in C++](https://static.tutorialandexample.com/cpp/tellg-in-cpp2.png)
Example 3:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string s = "tutorialsandexamples";
istringstream in(s);
string phrase;
in >> phrase;
cout << "After it reads the word \"" << phrase
<< "\" tellg() returns " << in.tellg() << '\n';
return 0;
}
Output
![Tellg in C++](https://static.tutorialandexample.com/cpp/tellg-in-cpp3.png)
Characteristics
Neither the file size nor the offset from the beginning in bytes are reported by tellg(). It only provides a token value that can be used to search for the same location in the future. (The type's ability to be converted into an integral type is not even assured)
This function returns the get pointer's current position if the pointer points to a valid location. If not, "-1" is returned.
Difference between tellg() and tellp():
- The current "put" position of the pointer in the stream is returned by tellg() function, which is utilized with output streams, whereas tellp() function returns the pointer's current "get" position in the stream and is used with input streams.
- The current character's position in the output stream is returned by tellg() function but the character's current position in the input stream is returned by tellp() function.
- The put pointer's location is provided by tellp() and the get pointer's position is provided by tellg().
- Syntax for tellp() is pos_type tellp();
Syntax for tellg() is pos_type tellg();
Use seekg() and tellg() to read a record from a file in C++
The task is to read the Kth record and carry out certain operations given that a binary file named "student.data" containing the records of 100 students has already been loaded into the computer's memory.
The iostream library, which is a component of the standard library, has a function called seekg() that lets you search for any location in a file. Setting the location of the subsequent character to be extracted from the input stream of a given file is done using it in file handling.
The current "get" position of the pointer in the stream is returned by the tellg() function, which is used with input streams. It takes no parameters and returns an integer data type value of the member type pos_type, which is the get stream pointer's current position.
Program
#include <iostream>
#include <fstream>
using namespace std;
class person {
int age;
char Name[20];
public:
void show(int x);
};
void person::show(int x) {
ifstream fin("person.dat", ios::binary);
if (!fin.is_open()) {
cout << "There is an error opening your file"<<endl;
return;
}
fin.seekg(x * sizeof(person));
fin.read((char*)this, sizeof(person));
cout << "Person age: " << age << endl;
fin.seekg(0, ios::end);
cout << "Present Location: " << fin.tellg() / sizeof(person) + 1
<< " of " << fin.tellg() / sizeof(person) << endl;
fin.close();
}
int main() {
int x = 5;
person p;
p.show(x);
return 0;
}
Output
![Tellg in C++](https://static.tutorialandexample.com/cpp/tellg-in-cpp4.png)