#include#include int main() { std::string filename = "example.txt"; std::ifstream file(filename); if (!file) { std::cerr << "Unable to open file: " << filename << std::endl; return 1; } std::string line; int count = 0; while (std::getline(file, line)) { count += file.gcount(); } std::cout << "Number of characters read: " << count << std::endl; return 0; }
#includeIn this example, we are reading input from the console using std::getline and then printing the number of characters read using the gcount function. This example demonstrates how gcount can be used to track the number of characters read from standard input. Package Library: Standard Libraryint main() { std::string input; std::getline(std::cin, input); std::cout << "You entered " << std::cin.gcount() << " characters." << std::endl; return 0; }