Reading in a file to a dynamically allocated array and changing the array's size when it is full

I'm trying to read in a file of unknown length into a dynamically allocated array, and anytime the array is full to call another function to double the array's length and continue reading in the file where it left off. I'm having trouble setting up the loops correctly and having a check to see when the array is full. What is a solution to create this functionality? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
  ifstream file(fileName);
if(file.is_open())
{
     for(int x = 0; x <arraySize; x++)
     {
          file >> myArray[x];
          if(x == myArray.size()-1 && !file.eof())
          {
               increaseArray(arraySize*2);
          }
     }
}
Last edited on
get the file's size first. Then allocate. Then read into one big buffer. Then parse it.

very, very roughly looks like...

ifstream ifs;
ifs.open(blah blah)

ifs.seek(eof);
filesize = ifs.tell(); //or is it tellg?? I can't remember.
//reset the file to beginning, theres a command for that also..

buff = new unsigned char[filesize];
ifs.read(buff, filesize);

Until your files are bigger than your available ram, this is much easier than reading chunks. Which means files over 10gb on today's computers.
Last edited on
Is there a more efficient way to do it? If I only want to read through the file once so I cannot get its size, and only increase the array size if the current size gets full, is there a way to do this?
Is there a more efficient way to do it?

Reading a file in one chunk is actually the most efficient way - if enough memory is available. Allocating and deleting memory and reading from the disk takes usually most of the time.
Do you have to use an array? Would be much easier to use a vector or even a string.

@jonnin,
not every computer has 10GB or more of ram. Even if it has it's normally not good practice if one program takes all the memory and leaves hardly anything for other programs.
I figured it out but now I am getting two memory leaks. I am deallocating the pointer array using delete[], but it seems to be something else. This is on an unrelated topic anyway so thanks for the help with my other issue.
Topic archived. No new replies allowed.