Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading Floats From Files

Started by waynesroom Aug 12, 2003 at 5:22 PM 1 replies 300+ views
Original Post
waynesroom
waynesroom
Basically, my racing game saves the best-lap time to a file and then loads it in again when the game restarts. I can save the lap-time to the file OK by sending it to an open file using fstream. The problem is loading it in again because the only way I know to load the lap-time is to use fstream to load it into a character array, when I actually need to load it in as a float data-type. I need a way to either load the value from the file directly into a float variable or a way to convert the character array into a float. I thought there might be a function similar to sprintf to do this but i don''t know. Any ideas??? thanx
Sneftel
Sneftel
If the number is stored in ascii representation in the file (i.e. some random number of base-ten digits long):

float f;
myfile >> f;

If the number is stored in binary representation (i.e. exactly 4 arbitrary bytes long):

float f;
myfile.get(reinterpret_cast(&f), sizeof(float));


How appropriate. You fight like a cow.
waynesroom
waynesroom
thanks very much, it works a treat

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.