#include#include using namespace std; int main() { ifstream myfile("example.txt"); char c; int count = 0; while (myfile.get(c)) { count++; } cout << "Number of characters read: " << myfile.gcount() << endl; myfile.close(); return 0; }
#includeIn this example, we open a file called "example.txt" using an ifstream object. We then read the first line of the file using the getline() function and store it in a character array called buffer. Finally, we use the gcount() function to print out the total number of characters read. The package library used in these examples is the C++ standard library.#include using namespace std; int main() { ifstream myfile("example.txt"); char buffer[100]; myfile.getline(buffer, 100); cout << "Number of characters read: " << myfile.gcount() << endl; myfile.close(); return 0; }