Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function named arrayToFile The function should accept three arguments: t

ID: 3767684 • Letter: W

Question

Write a function named arrayToFile

The function should accept three arguments:

the name of a file
a pointer to an int array
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
size of the array

The 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

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void fileTOArray(char *,int *,int);
void arrayTOFile(char *,int *,int);
main()
{
int a[100],n,i;
char *name;
clrscr();
cout<<"enter size of array"<<endl;
cin>>n;
cout<<"enter array elements"<<endl;
for(i=0;i<n;i++)
{
   cin>>a[i];
}
cout<<"enter file name"<<endl;
cin>>name;
arrayTOFile(name,a,n);
fileTOArray(name,a,n);
return 0;
}
void arrayTOFile(char name[],int a[],int n)
{
ofstream m;
m.open(name);
for(int i=0;i<n;i++)
{
m<<a[i];
}
cout<<"array elements are copied to file"<<endl;
m.close();
}
void fileTOArray(char name[],int a[],int n)
{
ifstream m;
char b[100];
m.open(name);
m>>b;
for(int i=0;i<n;i++)
{
a[i]=(int)b[i];
cout<<" "<<a[i];
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote