Hmm, hello! I am trying to read a binary file that contains a number of float values at a specific position. As seemingly must be done with binary files, they were saved as arrays of bytes, and I have been searching for a way to convert them back to floats with no success. Basically I have a char* memory block, and am attempting to extract the floats stored at a particular location and seamlessly insert them into a vector. I wonder, would that be possible, or would I be forced to rely on arrays instead if I wished to save copying the data? And how could it possibly be done? Thank you ^_^
1 Answer
If you know where the floats are you can read them back:
float a = *(float*)buffer[position];
Then you can do whatever you need of a, including 'push_back'ing it into a vector.
Make sure you read the file in binary mode, and if you know the positions of the float in the file it should work.
I'd need to see the code that generated the file to be more efficient.
5 Comments
Oh thanks! I could use that to push them back into the vector... Although would it be more efficient to somehow take them all and move them into a vector collectively? I don't have access to my code right now, but the format of the file is: unsigned vertexCount, unsigned UVCount, unsigned normalCount, float[] vertices, float[] UVs, float[] normals. I kinda just threw them all in together using fstream's write method ^//^
Nicolas Defranoux
The C++ vectors are pretty efficient, of course allocating an array an writing the floats in the array will be faster, but we are talking of a few ms there, so you can use a vector safely.