#include#include int main () { std::ifstream file ("example.txt"); if (file.is_open()) { std::cout << "File opened successfully!\n"; } else { std::cout << "Error opening file.\n"; } return 0; }
#includeIn this example, we are reading a file called "example.txt". We use the rdbuf() function to extract characters from the stream into a buffer. The library used in these examples is the C++ Standard Library.#include int main () { std::ifstream file ("example.txt"); if (file.is_open()) { std::streambuf* pbuf = file.rdbuf(); std::streamsize size = pbuf->pubseekoff(0, file.end, file.in); pbuf->pubseekpos(0, file.in); char* buffer = new char[size]; pbuf->sgetn(buffer, size); std::cout << buffer << "\n"; delete[] buffer; } return 0; }