In: Computer Science
Requirements:
C++
Array/File Functions
Write a function named arrayToFile. The function should accept 3
arguments:
The name of the file, a pointer to an array, and the size of the
array. The function should open the specified file in binary mode,
write the contents of the array to file, and then close the
file.
Write another function named fileToArray. This function should
accept 3 arguments: the name of the file, a pointer, to an int
array, and the size of the array. The function should open the
specified file in binary mode, read its content into the array, and
then close the file.
Write a complete program that demonstrates these functions by using
the arrayTofile, and then using the fileToArray function to read
the data from the same file. After the data are read from the file
into the array, display the arrays contents on the screen.
Additional Requirements:
Sample Run:
The source array: 8 6 7 5 3 0 9
The destination array: 8 6 7 5 3 0 9
ANSWER :-
GIVEN THAT :-
MODEL :-1
#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];
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, arrayContents, ARRAY_SIZE);
fileToArray(fileName, arrayContents2, 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();
}
MODEL:-2
# include <iostream>
# include <string>
# include <fstream>
using namespace std;
const int SIZE = 10;
bool arrayToFile(fstream&, int*, int);
bool fileToArray(fstream&, int*, int);
int main()
{
int arr[SIZE] = { 10, 8, 9, 7, 6, 4, 5, 3, 2, 1 },
arrTest[SIZE];
fstream file;
if (arrayToFile(file, arr, SIZE))
{
if (fileToArray(file, arr, SIZE))
{
for (int n = 0; n < SIZE; n++)
{
cout << arrTest[n] << " ";
}
cout << endl;
return 0;
}
}
return 1;
}
bool fileToArray(fstream &file, int* a, int
size)
{
file.open("t.dat", ios::in | ios::binary);
if (!file)
{
cout << "Can't open file t.dat\n";
return false;
}
file.read((char*)a, size);
file.close();
return true;
}
bool arrayToFile(fstream &file, int* a, int
size)
{
file.open("t.dat", ios::out | ios::binary);
if (!file)
{
cout << "Can't open file t.dat\n";
return false;
}
file.write((char*)a, size);
file.close();
return true;
}