#include <iostream>
#include <fstream>
int main() {
// Step 1: Declare and open a file stream
std::ofstream outFile("example.txt");
// Step 2: Check if the file stream is open
if (outFile.is_open()) {
// Step 3: Declare an array
int numbers[] = {1, 2, 3, 4, 5};
// Step 4: Determine the size of the array
int size = sizeof(numbers) / sizeof(numbers[0]);
// Step 5: Write array elements to the file
for (int i = 0; i < size; ++i) {
outFile << numbers[i] << " ";
}
// Step 6: Close the file stream
outFile.close();
// Step 7: Reopen the file for reading
std::ifstream inFile("example.txt");
// Step 8: Check if the file is open for reading
if (inFile.is_open()) {
// Step 9: Read array elements from the file
int readNumber;
while (inFile >> readNumber) {
std::cout << readNumber << " ";
}
// Step 10: Close the file stream
inFile.close();
} else {
std::cerr << "Error opening file for reading." << std::endl;
}
} else {
std::cerr << "Error opening file for writing." << std::endl;
}
return 0;
}