please help me!!

Started by
0 comments, last by hello_there 22 years ago
ok i can load number from a file into an array by loading every number seperatly. is there a way to load the whole thing in a few lines of code? i hope that makes sence
____________________________________________________________How could hell be worse?
Advertisement
Well depends how the file is stored (binary or text) but either way is easy enough. You just need to loop through the file loading each number as you find it (either in binary loading each number one after each other, or in text where you can load the number up until you reach a ''/0'', say). If you only have these numbers in the file then you can check at the start of each loop and exit when you have reached the end of the file, if you store other things in the same file then have a number at the start telling you how many numbers you have to load up and then loop that many times through the file.

void LoadIntoArray(int* Array)
{
ifstream fin("Filename.num");
int NumberToLoadIn = 0;
fin.read((char*)&NumberToLoadIn,sizeof(int));

if(Array)
delete Array;

if(NumberToLoadIn > 0)
{
Array = new int[NumberToLoadIn];
int Count = 0;

while(NumberToLoadIn > 0)
{
fin.read((char*)&Array[Count],sizeof(int));
count++;
}
}
else
{
Array = NULL;
}

fin.close();
}

Actually the loading into array line might be wrong, cant remember if you need the & or not, Im v tired, but that should work well enough (might want to create a new array in the func then return the address instead but either way)


Ballistic Programs

This topic is closed to new replies.

Advertisement