Loading Floats From Files
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement