#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Create an output file stream object and open a file named "output.txt"
ofstream outputFile("output.txt");
// Check if the file is successfully opened
if (!outputFile.is_open()) {
cout << "Error opening the file!" << endl;
return 1; // Exit the program if the file opening fails
}
// Write data to the file
outputFile << "Hello, this is some data written to the file." << endl;
outputFile << "This is another line in the file." << endl;
// Close the file stream
outputFile.close();
// Create an input file stream object and open the same file for reading
ifstream inputFile("output.txt");
// Check if the file is successfully opened for reading
if (!inputFile.is_open()) {
cout << "Error opening the file for reading!" << endl;
return 1; // Exit the program if the file opening for reading fails
}
// Read data from the file line by line and display it on the console
string line;
while (getline(inputFile, line)) {
cout << line << endl;
}
// Close the input file stream
inputFile.close();
return 0; // Exit the program indicating success
}