reading a file of unknown length with a function
- by Faken
I'm trying to write a short function that will let me quickly read in a file of unknown size and return pointer to the array of data and the length of that array but it seems my code isn't working. What am i doing wrong?
int readIn(int* pointer, param parameters, string description)
{
string fileName = parameters.fileName + " " + description + ".bin";
ifstream readFile;
readFile.open(fileName.c_str(), ios::in|ios::binary|ios::ate);
int size = readFile.tellg();
int length = size / 4;
int* output = new int [length];
readFile.seekg (0, ios::beg);
readFile.read(reinterpret_cast<char*>(output), (size));
readFile.close();
pointer = output; // link new array with the pointer
return length;
}
and in the main function:
int* testList;
int numEntries = readIn(testList, parameters, "test");
I end up with an error saying that my testList variable was used and not initialized. What am i doing wrong?