I wrote this code (below) following those guidelines:
(1) write a function named “arrayToFile”. The function should accept 3 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.
(2)write another function name “fileToArray”. This function should accept 3 arguments: 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, read its contents into the array, and then close the file.
(3)write a complete program that demonstrates these functions by using “arrayToFile” 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.
My code works perfectly but my concern is the following. After checking through the web I realize another way to do my code which is to make the function (1) and (2) after the main and before the main you put just :
void arrayToFile(string, int *, int);
void fileToArray(string, int *, int);
Well, in my case I made my functions (1) and (2) before the main. But apparently, both way works well.
My concerns are, I wonder what is the difference of doing either way before or after the main? Which one is the more efficient and when should I know I need to make my functions before the main or not?
Also another question is I do not see the point to have the array size in this program? Can you someone have a clue of his utility here ? (point the line I'm talking about in the code comment)
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
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void arrayToFile(string fileName, int *array, int sizeArray) //why the need of the array size ? I think the pb can be done without it
{
ofstream outputFile(fileName, ios::binary);
outputFile.write(reinterpret_cast<char *>(array), sizeArray); //.write member accepts pointer to char as first argument
// so use reinterpret_cast when calling it
outputFile.close();
}
void fileToArray(string fileName, int *array, int sizeArray)
{
ifstream inputFile(fileName, ios::binary);
inputFile.read(reinterpret_cast<char *>(array), sizeArray); //.read member accepts pointer to char as first argument
// so use reinterpret_cast when calling it
inputFile.close();
}
int main()
{
const int arraySize = 5;
int array[arraySize] = {1, 2, 3, 4, 5};
int arrayToRead[arraySize];
//call arrayToFile function to write into file
cout << "Let's start to write on the file from the array\n";
arrayToFile("ziziMechant.dat", array, sizeof(array));
cout << "Done\n";
//call fileToArray to read from file
cout << "Let's read that file to the array\n";
fileToArray("ziziMechant.dat", arrayToRead, sizeof(arrayToRead));
cout << "Job done\n";
//display the array we just read
cout << "Let's display the array content\n";
for (int i = 0; i < arraySize; i++)
{
cout << arrayToRead[i] << ",";
}
cout << "\n";
return 0;
}.
|