Jump to content
  • Advertisement
Sign in to follow this  

Reading FP32 files with C++

This topic is 3956 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); 
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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!