#include#include using namespace std; int main () { ifstream myfile ("example.txt"); if (myfile.is_open()) { char c; c = myfile.peek(); cout << "The next character is: " << c << endl; myfile.close(); } else cout << "Unable to open file"; return 0; }
#includeIn this example, we are using `peek()` in a loop to read each character in the file until the end of file marker is reached (`EOF`). The characters are then printed to the console. The `std::ifstream::peek()` function is part of the C++ standard library, which is included in the standard C++ package.#include using namespace std; int main () { ifstream myfile ("example.txt"); if (myfile.is_open()) { char c; while (myfile.peek() != EOF) { c = myfile.get(); cout << c; } myfile.close(); } else cout << "Unable to open file"; return 0; }