reading in floats from file??
Im trying to simply read in
a series of float #''s into an GLfloat[] array
from a file.
Im only generating points with them.
#1 how do I do this
#2 how do I do this no matter how many points there are in the
file.
the file looks like this:
210.5,656.997,87.9001
210.5,662.497,88.9001
210.5,667.997,89.9001
used in display list:
glNewList(UT,GL_COMPILE);
glColor3f(0.0f,1.0f,0.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, cylin);
glDrawArrays(GL_POINTS, 0, ????);
glEnd();
glEndList();
Im using C++ MFC coding.
Ive read as mony posts as I can on the subject but there are soooooooo many different answers.
TIA
Use fopen(), fgets(), strtok(), and atof(), all of which are standard C library functions.
i did like this:
my text file is as follows:
1.0
334.4
223.6
and i use the following code:
FILE * stream;
stream = fopen( "aerofiles/b2_vars.txt", "r" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fseek( stream, 0L, SEEK_SET );
fscanf( stream, "%d", &U[1] );
fscanf( stream, "%d", &V[1] );
// you can use also an array of course
fclose( stream );
}
glHorizon_Project

www.web-discovery.net
my text file is as follows:
1.0
334.4
223.6
and i use the following code:
FILE * stream;
stream = fopen( "aerofiles/b2_vars.txt", "r" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fseek( stream, 0L, SEEK_SET );
fscanf( stream, "%d", &U[1] );
fscanf( stream, "%d", &V[1] );
// you can use also an array of course
fclose( stream );
}
glHorizon_Project

www.web-discovery.net
i did like this:
my text file is as follows:
1.0
334.4
223.6
and i use the following code:
FILE * stream;
stream = fopen( "aerofiles/b2_vars.txt", "r" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fseek( stream, 0L, SEEK_SET );
fscanf( stream, "%d", &U[1] );
fscanf( stream, "%d", &V[1] );
// you can use also an array of course
fclose( stream );
}
glHorizon_Project

www.web-discovery.net
my text file is as follows:
1.0
334.4
223.6
and i use the following code:
FILE * stream;
stream = fopen( "aerofiles/b2_vars.txt", "r" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
fseek( stream, 0L, SEEK_SET );
fscanf( stream, "%d", &U[1] );
fscanf( stream, "%d", &V[1] );
// you can use also an array of course
fclose( stream );
}
glHorizon_Project

www.web-discovery.net
this is what I have working now:
void CMainFrame::reader()
{
GLfloat cylin[9042];
ifstream InputFile;
InputFile.open("test.txt", ios::in , filebuf::openprot);
numb = sizeof(InputFile);
for(int i = 0; (i < 9042) && !InputFile.eof(); ++i)
{
InputFile>>cylin;
}
InputFile.close();
}
This only works for whitespace between files though,
but it is simple.
Any ideas on how I find out how many floats are in my file??
so I can allocate the memory and render all the points in the file.
TIA
void CMainFrame::reader()
{
GLfloat cylin[9042];
ifstream InputFile;
InputFile.open("test.txt", ios::in , filebuf::openprot);
numb = sizeof(InputFile);
for(int i = 0; (i < 9042) && !InputFile.eof(); ++i)
{
InputFile>>cylin;
}
InputFile.close();
}
This only works for whitespace between files though,
but it is simple.
Any ideas on how I find out how many floats are in my file??
so I can allocate the memory and render all the points in the file.
TIA
September 17, 2001 05:02 PM
Count the number of Carriage Returns in your file and multiply it by 3
September 17, 2001 07:16 PM
have you considered doing this from binary file instead of from a ASCII encoded text? Seems to me that''d be easier and lead to smaller files at the same time... that and you could read and read until you hit the end of the file...
Rather than trying to figure out how much data is in the file, and then going into the loop, what you could do is have your array grow dynamically. The way that you do this would be to create an array at the beginning with a certain size, and then when you reach the size limit, you create a new array with a larger limit, copy the contents of the original array, then delete the old array. Then if memory usage is a concern, once you''ve gotten everything, you could allocate a new array that is the exact size of the data, copy it to that array, and delete the old array.
Example:
float *array;
int max_size = 256, pos = 0;
array = new float[max_size];
while( !feof() )
{
// make sure there is room in the array
if( pos == max_size )
{
int new_size = max_size * 2;
float *temp = new float[new_size];
memcpy( temp, array, sizeof(float) * max_size );
delete[] array;
array = temp;
max_size = new_size;
}
// input next element
...
}
If you use this method, you should pick a value for max_size that is larger than most of the data files you plan to read in, but not too large. Speed will only be hurt substantially if the array has to be reallocated several times, which would probably mean that the value you chose for max_size was too small.
j.w.
Example:
float *array;
int max_size = 256, pos = 0;
array = new float[max_size];
while( !feof() )
{
// make sure there is room in the array
if( pos == max_size )
{
int new_size = max_size * 2;
float *temp = new float[new_size];
memcpy( temp, array, sizeof(float) * max_size );
delete[] array;
array = temp;
max_size = new_size;
}
// input next element
...
}
If you use this method, you should pick a value for max_size that is larger than most of the data files you plan to read in, but not too large. Speed will only be hurt substantially if the array has to be reallocated several times, which would probably mean that the value you chose for max_size was too small.
j.w.
Just a note, thought it mite be useful, I used this code to load files as big as 600MB!
Basically I open a big or small file at the start of my program, close it at the end. Whenever I need a new line of data I ReadLine(); It should be easy to modify to what ever you want
It works for me so It mite b usefull to others.
Hope this helps someone.
Edited by - dark_creature on September 21, 2001 7:18:28 PM
Edited by - dark_creature on September 21, 2001 7:18:59 PM
Basically I open a big or small file at the start of my program, close it at the end. Whenever I need a new line of data I ReadLine(); It should be easy to modify to what ever you want
It works for me so It mite b usefull to others.
Hope this helps someone.
|
Edited by - dark_creature on September 21, 2001 7:18:28 PM
Edited by - dark_creature on September 21, 2001 7:18:59 PM
or you could change the ReadLine function so that it reads:
void ReadLine(void)
{
GLfloat icrd,jcrd,kcrd;
while (!feof(File))
{
//probably a better way, I use 60 chars to make sure
fscanf(File, "%f,%f,%f", &icrd, &jcrd, &kcrd);
myVector=Vector(icrd,jcrd,kcrd);
}
// here, to start reading the file again, if you loop
// through data
if (feof(File)&&(File))
{
rewind(File); // go back to start of file
}
}
this had no error checking, but it is cleaner than the former function...
Direwolf
void ReadLine(void)
{
GLfloat icrd,jcrd,kcrd;
while (!feof(File))
{
//probably a better way, I use 60 chars to make sure
fscanf(File, "%f,%f,%f", &icrd, &jcrd, &kcrd);
myVector=Vector(icrd,jcrd,kcrd);
}
// here, to start reading the file again, if you loop
// through data
if (feof(File)&&(File))
{
rewind(File); // go back to start of file
}
}
this had no error checking, but it is cleaner than the former function...
Direwolf
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement