Trouble with reading/writing integer data to/from binary file

I'm completing an assignment that involves writing an array of integer data to a binary file, then reading the integer data from that binary file back into an array. The issue is that when I try to display the array that I've read the int data back into, it only has the first integer, and the rest seems to be "garbage" data (specifically, all elements in the array other than the first element will display the value -858993460. Here are my steps:

--Value an array (out_array) of 650 elements with random numbers from 1 to 100
--Display the out_array to confirm it is valued correctly
--Write that integer out_array data to a binary file
--Read the integer data from the above binary file back into another array (in_array)
--Display the in_array to confirm I've done this correctly

I'm confident I'm valuing the out_array correctly. I suspect something is incorrect either in how I'm writing that array to the binary file (perhaps I'm not actually writing the entire array), or in how I'm reading the file back into my in_array.

Would appreciate any help, I'm new to binary files and have been at this for hours. Thanks in advance.

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
	}
}
Last edited on
The problem seems to be in void value_in_array(fstream &infile, int arr[row_size]), sizeof(arr) is only 4, because when you pass an array to a function only the address of the first element is passed.
This should work
infile.read(reinterpret_cast<char *>(arr), sizeof(int) * row_size); //Read int values from file into array
Thomas, you rock. Not only did this help me see the bug, it also helped me understand something about how arrays are passed into functions that my textbook completely omitted - it didn't at all mention that only the address of the first element is passed. Thanks so much!
Topic archived. No new replies allowed.