std::ifstream file("example.txt"); std::string data; if (file.is_open()) { file.seekg(0, std::ios::end); data.resize(file.tellg()); file.seekg(0, std::ios::beg); file.read(&data[0], data.size()); file.close(); } std::cout << data;
std::ifstream file("binary.dat", std::ios::binary); int value; if (file.is_open()) { file.read(reinterpret_castIn this example, the function reads binary data from a file called "binary.dat". The file is opened in binary mode by passing in the std::ios::binary flag as the second parameter. The read function takes a char pointer to the memory location where the value will be stored, and the size of the value. Overall, the std ifstream read function is a useful tool for reading data from files in C++.(&value), sizeof(value)); file.close(); } std::cout << "The value is: " << value;