TELLG()
This is used with input file streams, such as ifstream, to determine the current position of the get pointer (read pointer) within a file. The position returned by tellg() is measured in bytes from the beginning of the file, with counting starting at 0, not 1. This means the very first byte in the file corresponds to position 0. tellg() is useful for tracking how much of the file has been read, calculating remaining data, or determining the file size when combined with seekg(). By providing precise information about the read pointer’s location, it enables accurate control for reading and navigating through files.
// Returns the current position of the get (read) pointer
istream::pos_type tellg();
* istream::pos_type is a type representing positions within a file stream.
* tellg() is typically used with ifstream (input file stream) or any istream-derived object.#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("example.txt");
if (!file) {
cout << "Could not open the file.\n";
return 0;
}
//0 is the beginning of the file
file.seekg(0, ios::end); // This means pointer is now pointing at the last character of the file
// Get the current position (which is the size of the file in bytes)
int size = file.tellg();
cout << "The file size is: " << size << " bytes\n";
file.close();
return 0;
}FINDING A CHARACTER POSITION
FINDING CHARACTER POSITION: EXAMPLE 2
Last updated