Question

In: Computer Science

Requirements: C++ Array/File Functions Write a function named arrayToFile. The function should accept 3 arguments: The...

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:

  • You may not use a loop to write the array nor read the array.   Writing and reading the array must be done as one operation.
  • Neither function interacts with the user in anyway.
  • The functions return 0 if the file opened successfully, -1 otherwise.
  • Each function is separately responsible for opening and closing the file when they access it.

Sample Run:

The source array: 8 6 7 5 3 0 9

The destination array: 8 6 7 5 3 0 9

Solutions

Expert Solution

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;
}


Related Solutions

Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a function named hasNValues which takes an array and an integer n as arguments. It...
Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. If you are writing in Java or C#, the function signature is int hasNValues(int[ ] a, int n) If you are writing in C or C++, the function signature is int hasNValues(int a[ ], int n, int len) where len is the length of a Note that an array...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the...
2 Write a function named equivalentArrays that has two array arguments and returns 1 if the two arrays contain the same values (but not necessarily in the same order), otherwise it returns 0. Your solution must not sort either array or a copy of either array! Also you must not modify either array, i.e., the values in the arrays upon return from the function must be the same as when the function was called. Note that the arrays do not...
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
Write scores of 20 students on a quiz to a file. The file should be named...
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write code to define a function named mymath. The function has three arguments in the following...
Write code to define a function named mymath. The function has three arguments in the following order: Boolean, Integer, and Integer. It returns an Integer. The function will return a value as follows: 1. If the Boolean variable is True, the function returns the sum of the two integers. 2. If the Boolean is False, then the function returns the value of the first integer - the value of the second Integer.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT