Loading Floats From Files

Started by
1 comment, last by waynesroom 20 years, 8 months ago
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
Advertisement
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.
thanks very much, it works a treat

This topic is closed to new replies.

Advertisement