Reading in files as int/float array or one String/char[] ( OpenGL Data )

ParticlePeter ParticlePeter at gmx.de
Mon Dec 12 01:31:42 PST 2011


Hi,

I read several posts about reading in files, but I can't find the "best" way for my purpose. I am using std.file in the examples bellow.

1.) I'm trying to read an OpenGL Vertex and Fragment Shader. The wired thing on my approach is that it works ... sometimes, but sometimes the Shaders cannot be compiled. 
So how should I read a file as one complete char[] in a safe way ? This is my way:
  auto fileFrag = cast( GLchar[] )readText( "Shader/Shader_01.frag" ) ;
  const GLchar * fragSource = cast( GLchar * )fileFrag ;

2.) I am reading in Vertex Data from a file. It holds only floats, e.g.:
-38.3887 97.7612 -10.5231 
-38.3572 98.8543 -10.5064 
...
There is no metadata in the file giving information about the count of values. I use a dynamic array, and append each line to my array, which I think is not the most clever or efficient approach:
  vertices = new GLfloat[0] ;
  auto file = File( "Data/Vertices_01.txt" ) ;
  foreach( line ; file.byLine() )
    vertices ~= to!( GLfloat[] )( line.split ) ;
  file.close() ;

Can I get all floats ( words as float ) from a file at once without using file.byLine into a Dynamic array ( or fixed array, I know the word count ) ? If not, what would be the most efficient approach for these kind of files ( they might get huge ) ?

3.) When reading a file line by line it would be nice to have the count of Lines of the file ( numLines ) and the line number in a variable, so I could random access a fixed size array with this line number. I tried this code, which works for arrays, but unfortunately not for lines ( for me ).
  GLfloat arrVertex[ numLines ][ 3 ] ;
  foreach( i , line ; file.byLine() )
    arrVertex[ i ] = to!( GLfloat[3] )( line.split ) ;

How can I get the Line count from a file, and how can I get the line numbers without initializing a counter in front of the foreach loop and increase it manually inside the loop ?

Thank you for any advice,

Cheers, ParticlePeter !


More information about the Digitalmars-d-learn mailing list