array sizing for file input

Started by
7 comments, last by pacrugby 22 years, 7 months ago
Id like to thank everyone who has helped me figure things out. reading files in is messed up because there are so many ways to do it. I think I have a handle on it now. Now for my question: using VC++6. I read in my file and the first 2 numbers read in are the sizes I want my arrays to be. InputFile >> n1; InputFile >> n2; GLfloat array1[n1]; GLint array2[n2]; so I want to initialise the arrays to these sizes but I have to initialise them in the .h file (ie array1[10000])so I can access them in my vertex array function. any suggestions on setting this up?
Advertisement
Look up the new operator or the malloc function. Make sure to also look up delete and/or free, otherwise your program will leak memory all over the place.
Get a towel and wipe up all that leaked memory!

    InputFile >> n1;InputFile >> n2;GLfloat *Array1 = new GLfloat [ n1 ];GLint   *Array2 = new GLint [ n2 ];    


when you're done :
        // Make sure that Array1 and Array2 exist before trying to delete! (Or there will be trouble)if (Array1) delete [] Array1; if (Array2) delete [] Array2;    



I hope that this helps!


Feel free to email me.

Edited by - Lord Karnus on September 19, 2001 12:54:22 AM
[email=JValentine_13@hotmail.com]contact[/email]
thanks but Im still having errors:

InputFile>>nodes;
InputFile>>elements;
GLfloat *cylin = new GLfloat [nodes*3];
GLint *indi = new GLint [elements*3];

this seems to work. but after I finish using the
read in function and enter my display list
the pointers to cylin and indi change
so when I call
glDrawElements(GL_TRIANGLES, elements*3,GL_UNSIGNED_INT, indi);
code:
VC++6.0
void CMainFrame::reader()
{
// int k;
GLfloat u[3],v[3],n[3];
ifstream InputFile;
ifstream InputFile2;

InputFile.open("input.txt", ios::in , filebuf::openprot);
InputFile>>nodes;
InputFile>>elements;
GLfloat *cylin = new GLfloat [nodes*3];
GLint *indi = new GLint [ elements*3 ];



InputFile.close();
InputFile.open("test.txt", ios::in , filebuf::openprot);
for(int i = 0; (i < nodes*3) && !InputFile.eof(); ++i)
{
InputFile>>cylin;
}
InputFile.close();
InputFile2.open("elemout.txt", ios::in , filebuf::openprot);
for(i = 0; (i < elements*3 ) && !InputFile2.eof(); ++i)
{
InputFile2>>indi;<br> indi = indi-1;<br> }<br>InputFile2.close();<br>}<br><br> </i>
Also you may want to set the variables Array1 and Array2 to NULL
as when they are deleted you can still reference them

eg..
delete Array1
...//later on after deletion

GLint i = Array2[2];

would still be ok by the compilerand at run time but you would get garbage values. Where as if it was null the computer would complain.

Hope this helps

From what I can make of your code, cylin and indi are local variables. They lose scope before you get to the display list (and thus are nonexistent).
  // to put your code in nice white boxes like this one, pre- and postfix// them with source and /source tag pairs (GameDev.Net tags are enclosed// in square brackets: []). To just keep the spacing, put them in code// and /code tag pairs  
   just testing out what you said   


Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
my reccomendation (for simplicity) is use apVector.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
my reccomendation (for simplicity) is use apVector.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"

This topic is closed to new replies.

Advertisement