Monday, July 3, 2017

Programming Challenge 12.8 - Array/File Functions

/* Array/File Functions - This program contains a function named arrayToFile.
    It accepts three arguments:
   
        * The name of a file
        * A pointer to an int array
        * The size of the array

    The function opens the specified file in binary mode, writes the contents
    of the array to the file, and the closes the file.

    It also contains another function named fileToArray. This functions accepts
    three arguments:

        * The name of a file
        * A pointer to an int array
        * The size of the array

    The function opens the specified file in binary mode, reads its contents
    into the array, and then closes the file.

    The functions are demonstrated in this program, 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, the array's contents are displayed on the screen. */

#include "Utility.h"

void rNumGenerator(int *, const int);
int  arrayToFile(const string fileName, int *, const int);
int  fileToArray(const string fileName, int *, const int);
void displayArray(const int *, const int);

int main()
{
    const int NUMELS = 6;

    int     lotteryNumbers[NUMELS] = { 0 };
    int     fOpenErr = 0;
    string fileName = "";

    cout << "ARRAY/FILE FUNCTIONS.\n\n"
          << "This program demonstrates two functions. One writes an array\n"
          << "containing RNG-numbers to a file in binary mode, the other reads\n"
          << "the contents of the file into an array. If both operations are\n"
          << "successful, the contents of the array is displayed.\n\n";

    cout << "Enter the name of a file to write to: ";
    cin >> fileName;

    fOpenErr = arrayToFile(fileName, lotteryNumbers, NUMELS);

    if (fOpenErr != -1)
    {
        cout << "Enter the name of a file to read from: ";
        cin >> fileName;

        fileToArray(fileName, lotteryNumbers, NUMELS);
    }

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: rNumGenerator

    This function accepts a pointer to an array and its size
    as parameters. It fills the array with 6 random numbers.
   ********************************************************** */

void rNumGenerator(int *lotteryNums, const int NUMELS)
{
    const int HIGHEST_NUM = 45,
               LOWEST_NUM = 1;

    int lastNum = 0;
    int count = 0;
   
    srand((unsigned int)time(0));

    while (count < NUMELS)
    {
        lastNum = (rand() % (HIGHEST_NUM - LOWEST_NUM + 1) + LOWEST_NUM);
       
        if (lastNum != 0)
        {
            *(lotteryNums + count) = lastNum;
        }

        count++;
    }
}

/* **********************************************************
   Definition: arrayToFile

    This functions accepts the name of a file, a pointer to an
    integer array, and the size of the array as parameters. It
    opens the file in binary mode, writes the contents of the
    array to the file, and the closes the file.
   ********************************************************** */

int arrayToFile(const string fileName, int *lotteryNums, const int NUMELS)
{
    char *arrPtr = nullptr;

    rNumGenerator(lotteryNums, NUMELS);

    fstream toFile(fileName, ios::out | ios::binary);

    if (!toFile.fail())
    {

        cout << "\nWriting array data to file ...\n";
        toFile.write(reinterpret_cast<char *>(lotteryNums), sizeof(lotteryNums));
        cout << "Array contents successfully written to file.\n"
              << "Now closing " << fileName << "\n\n";
    }
    else
    {
        cout << "\nFile Writing Error. Array contents could not be written to file ...\n"
              << "Press enter to close this program.";
        return -1;
    }
    toFile.close();

    return 0;
}

/* **********************************************************
   Definition: fileToArray

    This functions accepts the name of a file, a pointer to an
    integer array, and the size of the array as parameters. It
    opens the file in binary mode, reads its contents into the
    array, and then closes the file.
   ********************************************************** */

int fileToArray(const string fileName, int *lotteryNums, const int NUMELS)
{
    char *arrPtr = nullptr;

    fstream fromFile(fileName, ios::binary | ios::in);
   
    if (!fromFile.fail())
    {
        cout << "\nReading in file-contents ...\n";
        fromFile.read(reinterpret_cast<char *>(lotteryNums), sizeof(lotteryNums));
        cout << "File contents successfully read into array.\n"
              << "Now closing " << fileName << "\n\n";

        cout << "\nThis is the array's contents: ";
        displayArray(lotteryNums, NUMELS);
    }
    else
    {
        cout << "File Reading Error. Could not read " << fileName << " ...";
        return -1;
    }
    fromFile.close();

    return 0;
}

/* **********************************************************
   Definition: displayArray

    This functions accepts a constant pointer to an integer
    array, and the size of the array as parameters. It outputs
    the contents of the array to screen.
   ********************************************************** */

void displayArray(const int *lotteryNums, const int NUMELS)
{
    for (int i = 0; i < NUMELS; i++)
    {
        cout << *(lotteryNums + i) << " ";
    }
}

Example Output:






No comments:

Post a Comment