1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <iostream>
#include <fstream>
using namespace std;
// constant variables
const int ARRAY_SIZE = 10;
const int NAME_SIZE = 20;
// function prototypes
void arrayToFile(char[], int[], int);
void fileToArray(char[], int[], int);
int main()
{
int arrayContents[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int arrayContents2[ARRAY_SIZE];
int* intPointer2;
int* intPointer;
intPointer = arrayContents;
intPointer2 = arrayContents2;
char fileName[NAME_SIZE];
cout << "Chapter 12 Program 8" << endl << endl;
cout << "Enter name of file." << endl << endl;
cin.getline(fileName, NAME_SIZE);
arrayToFile(fileName, intPointer, ARRAY_SIZE);
fileToArray(fileName, intPointer2, ARRAY_SIZE);
//dislplay contents of arrayContents2
for (int count = 0; count < ARRAY_SIZE; count++)
{
cout << intPointer[count] << " ";
}
return 0;
}
void arrayToFile( char file[], int pointer[], int arr_size)
{
fstream dataFile;
dataFile.open(file, ios::in | ios::binary);
for (int count = 0; count < arr_size; count++)
{
dataFile.write(reinterpret_cast<char *>(pointer), sizeof(pointer));
}
dataFile.close();
}
void fileToArray( char file[], int pointer[], int arr_size)
{
fstream dataFile;
dataFile.open(file, ios::out | ios::binary);
for (int count = 0; count < arr_size; count++)
{
dataFile.read(reinterpret_cast<char *>(pointer), sizeof(pointer));
}
dataFile.close();
}
|