// Attached: Lab_6 Program #2
// ===========================================================
// File: Lab6_Program2.cpp
// ===========================================================
// Programmer: Elaine Torrez
// Class: CS 1A
// ===========================================================
#include <iostream>
#include <fstream>
using namespace std;
// ==== main ===================================================
// This program writes an array of integers to a binary file
// and then reads the data back into memory and displays it.
//
// Input:
// No user input.
//
// Output:
// The numbers 1 through 10 are written to a binary file and
// then displayed on the screen after being read back.
// ===========================================================
int main()
{
const int SIZE = 10;
int numbers[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream file;
// open the file for binary output
file.open("test.bin", ios::out | ios::binary);
cout << "Writing the data to the file.\n";
// write the array to the binary file
file.write(reinterpret_cast<char *>(&numbers), sizeof(numbers));
file.close();
// open the file for binary input
file.open("test.bin", ios::in | ios::binary);
// read the contents of the file into the array
cout << "Now reading the data back into memory.\n";
file.read(reinterpret_cast<char *>(&numbers), sizeof(numbers));
// display the contents of the array
for (int count = 0; count < SIZE; count++)
{
cout << numbers[count] << " ";
}
cout << endl;
file.close();
return 0;
} // end of main()
// ===========================================================