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.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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));
constint file_size = file_size_temp;
//Define array to hold file values
//THIS LINE CAUSES A COMPILER ERROR
int main_array[file_size];
return 0;
}
@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?
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()
{
constint array_size = 2000;
// Use a static to use heap memory.
staticint 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;
}