reading a file of unknown length with a function

Posted by Faken on Stack Overflow See other posts from Stack Overflow or by Faken
Published on 2010-03-19T16:24:38Z Indexed on 2010/03/19 16:31 UTC
Read the original article Hit count: 233

Filed under:

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?

© Stack Overflow or respective owner

Related posts about c++