Archived

This topic is now archived and is closed to further replies.

hello_there

please help me!!

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites