In Tony Gaddis Starting out with c++ eight edition, how would I go about doing P
ID: 3801411 • Letter: I
Question
In Tony Gaddis Starting out with c++ eight edition, how would I go about doing Programming Challenge # 8 in chapter 12? Could all the steps be listed? I do not know where to begin.
Thank you!
Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The NOTE: Using an editor, you should create a simple text file that can be used to test this program. NOTE: Using an editor, you should create a simple text file that can be used to test this program. NOTE: Using an editor, you should create a simple text file that can be used to test this program. Review Questions and Exercises 703 function should open the specified file in binary mode, read its contents into the array, and then close the file. Write a complete program that demonstrates these functions by using the arrayToFile function to write an array to a file, 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 array’s contents on the screen
Explanation / Answer
// C++ code
#include <iostream>
#include <cmath>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#define SIZE 10
void arrayToFile(int inputArray[],FILE * filePointer,int size)
{
fwrite(inputArray,sizeof(int),size,filePointer);
return;
}
void fileToArray(int inputArray[],FILE *filePointer,int size)
{
fread(inputArray,sizeof(int),size,filePointer);
}
int main()
{
int inputArray[SIZE];
int i;
cout << "input 10 integers: ";
for(i=0;i<10;i++)
{
cin >> inputArray[i];
}
FILE *filePointer;
filePointer=fopen("output.txt","wb");
arrayToFile(inputArray,filePointer,SIZE);
fclose(filePointer);
int resultArray[SIZE];
FILE *filePointer2;
filePointer2=fopen("output.txt","rb");
fileToArray(resultArray,filePointer2,SIZE);
fclose(filePointer2);
cout << "integers read from file: ";
for(i=0;i<10;i++)
{
cout << resultArray[i] << endl;
}
return 0;
}
/*
output:
input 10 integers:
1 2 3 4 5 6 7 8 9 10
integers read from file:
1
2
3
4
5
6
7
8
9
10
output.txt
0100 0000 0200 0000 0300 0000 0400 0000
0500 0000 0600 0000 0700 0000 0800 0000
0900 0000 0a00 0000
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.