Streams (ReadFloat)

Blitz3D Forums/Blitz3D Beginners Area/Streams (ReadFloat)

_PJ_(Posted 2005) [#1]
When I use this code:



The data is read, although as Integers.

If I change to using EITHER ReadFloat(File) OR ReadFloat#(File)

The data is NOT read correctly.

My data is as follows:

3.0000000
1.0000000
-3.0000000
-3.0000000
1.0000000
-3.0000000
0.0000000
-2.7000000
-11.000000
0.0000000
10.300000
18.000000
0.0000000
9.7000000
18.000000
0.0000000
0.8000000
-23.600000


Any ideas on why this is?


BlackJumper(Posted 2005) [#2]
You have not written floats to the file... you have written a text representation of floats.

You will need to parse the text file and convert the strings from each line to a float before assigning it in your array.

something like...
File=OpenFile("File.txt")

Dim a#(18)
For f=1 To 18
	temp$=ReadLine(Decal)
                convert# = Float(temp$)
	a#(f)=convert#
Next
	
CloseFile(File)


... untested, but should put you on the right line


Shambler(Posted 2005) [#3]
Or you could use WriteFloat/ReadFloat instead which is more efficient and easier to read ( by you not blitz ;) )

[edit]

Looks a bit like this, keeps it nice and clear

Example of writing file

Function Save_Ini()

file=WriteFile("dme.ini")
If file=0 Then RuntimeError ("Unable to Save ini file")
;Video
WriteInt (file,gGModeSelected)
WriteInt (file,gGamma)
WriteInt (file,gVSync)
WriteInt (file,gDetail)
;Sound
WriteFloat (file,gSoundVolume#)
WriteFloat (file,gMusicVolume#)
...

Example of reading file

Function Load_Ini()

file=ReadFile("dme.ini")
If file=0 Then RuntimeError ("Unable to Load ini file")
;Video
gGModeSelected=ReadInt(file)
gGamma=ReadInt (file)
gVSync=ReadInt (file)
gDetail=ReadInt(file)
;Sound
gSoundVolume#=ReadFloat(file)
gMusicVolume#=ReadFloat(file)
...



big10p(Posted 2005) [#4]
Also, the problem with writing floats as strings is that sometimes float values are written/displayed using an exponent notation. In such cases, I don't think you can convert back to a float by simply doing x# = Float(val$). So, do what Shambler says.


Koriolis(Posted 2005) [#5]
Yes you can.
bla# = "1.25e-4".toFloat()
Try it, it does work.


big10p(Posted 2005) [#6]
I stand corrected. :)