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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
//Global constants
const int32_t row_size = 650
, min_num = 1
, max_num = 100;
//Function declarations
void rand_array(int32_t arr[row_size], int32_t row_size);
void show_array(int32_t arr[row_size], int32_t row_size);
void count_ints(fstream &infile);
void value_in_array(fstream &infile, int arr[row_size]);
int main()
{
int32_t out_array[row_size];
int32_t in_array[row_size];
srand(time(0));
//Value array with numbers
rand_array(out_array, row_size);
//Display array
show_array(out_array, row_size);
//Create and open out file to write data to it
fstream outfile;
outfile.open("binfile.bin"
, ios::out | ios::binary);
//Write data to file and close it
outfile.write(reinterpret_cast<char *>(out_array), sizeof(out_array));
outfile.close();
//Open file and count the number of ints in the binary file as a way to check the program
fstream infile;
infile.open("binfile.bin"
, ios::in | ios::binary);
count_ints(infile);
infile.close();
//Read values from file into array
infile.open("binfile.bin"
, ios::in | ios::binary);
value_in_array(infile, in_array);
//Display the "in_array"
show_array(in_array, row_size);
//Close file
infile.close();
return 0;
}
//*********************************************
//Function to value array with random numbers *
//*********************************************
//IMPORTANT: You must place the srand function in the main function for this to work
void rand_array(int32_t arr[row_size], int32_t row_size)
{
for (int32_t row_num = 0; row_num < row_size; row_num++)
{
arr[row_num] = (rand() % (max_num - min_num + 1)) + min_num;
}
}
//***************************
//Function to display array *
//***************************
void show_array(int32_t arr[row_size], int32_t row_size)
{
for (int32_t row_num = 0; row_num < row_size; row_num++)
{
cout << arr[row_num] << "\t";
if (row_num != 0 && (row_num + 1) % 5 == 0)
cout << endl;
}
cout << endl;
}
//**********************************
//Function to count ints from file *
//**********************************
void count_ints(fstream &infile)
{
int32_t value //Fixed width integer of 4 bytes to hold integer value from the file
, count = 0;
//Use loop to read contents of file as integers, incrementing a counter variable for each integer
while (infile.read(reinterpret_cast<char *>(&value), sizeof(int32_t)))
{
//Update the count each time a value from the file is read
count++;
}
//TEST STUB
cout << "The number of integers in the file is " << count << endl;
}
//*********************************************************
//Function to read integer values from file into an array *
//*********************************************************
void value_in_array(fstream &infile, int arr[row_size])
{
if (infile)
{
infile.read(reinterpret_cast<char *>(arr), sizeof(arr)); //Read int values from file into array
infile.close(); //Close file
}
//If file does not open, display error message and exit the program
else
{
cout << "Error, the file was not successfully opened. Press enter to exit program" << endl;
cin.get(); //Allow for pause so that user can read message before pressing enter
exit(0); //Exit program
}
}
|