#includeusing namespace std; int main() { ifstream input("file.txt"); if (input) { string line; while (getline(input, line)) { cout << line << endl; } input.close(); } else { cout << "Error: Cannot open file." << endl; } return 0; }
#includeusing namespace std; int main() { string data = "apple pear orange"; istringstream input(data); string fruit; while (input >> fruit) { cout << fruit << endl; } return 0; }
#includeIn this example, we read binary data from a file named "data.bin" that contains a series of MyData structs. We use the read function to read each struct as a block of bytes, and then cast it to the proper type. Package Library: Standard Library.using namespace std; struct MyData { int value; char name[16]; }; int main() { ifstream input("data.bin", ios::binary); if (input) { MyData data; while (input.read((char*)&data, sizeof(data))) { cout << "Value: " << data.value << ", Name: " << data.name << endl; } input.close(); } else { cout << "Error: Cannot open file." << endl; } return 0; }