Sign in to follow this  

Reading FP32 files with C++

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I want to read in a binary file which stores a N x N heightmap as stream of 32Bit floating point numbers. Every floating point number in the file is one height of the terrain. I tried the following:
FLOAT*    heightData; 
std::ifstream heightmapStream(str.c_str(), std::ios::binary); 
heightmapStream.seekg(0, std::ios::end); 
UINT vertexCount = heightmapStream.tellg(); 
vertexCount /= 4; 
heightmapStream.seekg(0, std::ios::beg); 

// Load height data 
heightData = new FLOAT[vertexCount]; 
heightmapStream.read( (char*)heightData, vertexCount*4); 
heightmapStream.close();
I open the file in binary modus, read the number of bytes and divide this number by 4 (because each height is 4 bytes long). Then I just read the whole file with read() by casting the float array heightData to a char*. Since I have some strange bugs in my application I am not sure if this is the right way. Thanks for help

Share this post


Link to post
Share on other sites
Why is FLOAT in upper-case?

Also, if you want to be a bit safer, you should use 'sizeof(FLOAT)' instead of '4'. Or at least add a static assertion that 'sizeof(FLOAT)==4'.

[EDIT]Ignore the following line. std::ios::in isn't actually necessary...
>>"std::ios::binary" should probably be "std::ios::in | std::ios::binary".

Share this post


Link to post
Share on other sites

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this