Turning a non-constant value into a constant

I'm writing a program where I need to count the number of integers in a given file, then pass those values into an array (unfortunately I can't use vectors for this assignment, which is why I have the problem I'm about to describe).

The issue is that when I define the array that will hold the file values, I have to specify the size of the array as a literal/constant. However, the number of values in the file is unknown, and I have to count them within the program. Once I count the number of integers in the file, I have to somehow use that same count to define the array, but it isn't really a constant.

My general approach is to use a function to return the number of integers in the file. Where I'm stuck is in how I can use that returned value to define the size of my array. I've seen the count of values and could theoretically hardcode that number as the array size, but I'd prefer to avoid that if possible. I looked into using const cast, but that seems to require pointers, and we haven't yet learned that.

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

//Global constants
const string infile_name = "file.bin";

//Function to open file and test file open
ifstream open_file(string file_name)
{
	//Open file
	ifstream infile (file_name, ios::in | ios::binary);

	//Test file open
	if (!(infile))
	{
		cout << "The file did not open successfully.  Press enter to close the program." << endl;
		cin.get();
		exit(0);
	}
	//TEST STUB
	else
		cout << "The file has been successfully opened" << endl;

	return infile;
}


//Function to return the number of integers in a binary file
int count_ints(ifstream &file_object)
{
	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 (file_object.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; 

	return count;
}

//Main function
int main()
{
	int file_size_temp;

	//Open the file
	open_file(infile_name);

	//Determine the number of integers in the array, in order to determine the needed array size
	file_size_temp = count_ints(open_file(infile_name));
	const int file_size = file_size_temp;    

        //Define array to hold file values
        //THIS LINE CAUSES A COMPILER ERROR
	int main_array[file_size];   

	return 0;
}
Hello mastakhan,

You could write line 63 as int *main_array = new int[file_size],

http://www.cplusplus.com/doc/tutorial/dynamic/

Hope that helps,

Andy
Last edited on
if your file has only numbers the reinterpret_cast is not required:
http://stackoverflow.com/questions/2421858/function-that-counts-the-number-of-integers-in-a-text-file
@Handy Andy
Unfortunately we've not learned pointers, and can't use that in this program. Are you aware of any way to accomplish this without using pointers?
Last edited on
In C++ array sizes must be compile time constants, the only ways around this fact is to use dynamic memory (which involves pointers), using std::vector (which you said you can't), or use a large array size (a size that should be large enough for this assignment).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
   const int array_size = 2000;
   // Use a static to use heap memory.
   static int main_array[array_size];   

   std::ostream fin("FILENAME");
   // Read loop.
   int counter = 0;
   // Make sure to check that counter is within range of the array.
   while(counter < array_size && fin >> main_array[counter])
      counter++;

   return 0;
}



Understood jlb - thanks for the info.
Make it bigger than you are likely to ever need. If you think you need 1000, make it 5000 or something.

Topic archived. No new replies allowed.