File Hadnling Once Again...

Blitz3D Forums/Blitz3D Beginners Area/File Hadnling Once Again...

Apollonius(Posted 2003) [#1]
File line 15 = "Gamma = 0.5" <- in the file
However I need to get the value 0.5 and put it in variable "Gamma_Num" and display it in the text field, but I need to able to change it, cause currently I can't get the variable and currently cant change the value in the text box, I got no clue what to do right now :|

Make ur explication simple so I can understand :D

AppTitle "Coalition Legions Game Commander"

	Global   fileopen  = False
	Global   Gamma_Num = 0

	Const    MAXLINES  = 127

	; Create Main Window
	w% = 300;GadgetWidth(Desktop()) - 100
	h% = 300;GadgetHeight(Desktop()) - 100
	x% = (ClientWidth(Desktop()) - w) / 2
	y% = (ClientHeight(Desktop()) - h) / 2
	wndMain = CreateWindow("               Coalition Legions Game Commander", x, y, w, h,0,17)
	

	
	ChildPanel = CreatePanel(2,20,280,220,wndMain,0)
	MainSlid   = CreateSlider(275+1,10,15,228,wndMain,2)
	SaveBut    = CreateButton("Save",5,250,60,20,wndMain)
	ExitBut    = CreateButton("Exit",70,250,60,20,wndMain)
	
	lab_Gamma  = CreateLabel("Gamma :",15,14,60,15,ChildPanel)
	txt_Gamma  = CreateTextField( 150,8,100,20,ChildPanel)
	CreatePanel(5,30,255,4,ChildPanel,1)
	;CreatePanel(135,5,4,255,ChildPanel,1)
	
		CreateLabox("Edit",2,7,270,230,30,1,wndMain)
	
	Dim dat$(MAXLINES)
	
	ReadCFG   = ReadFile ("dfv.cfg")
	linecount=0
	If  ReadCFG = 0 Then
		Notify "Unable to access the file needed!"
		End
	Else
		fileopen=True	
	End If
	;-------------------------
	; READ
	While Not Eof(ReadCFG)
		linecount=linecount+1
		dat$(linecount)=ReadLine(ReadCFG)
		If linecount=MAXLINES Exit 
	Wend
	CloseFile ReadCFG
	Gamma_Num = dat$(15)
	;------------------------
	CreateTimer(60)
quit=False
Repeat
	
	WaitEvent()
	; Close Window
	If EventID()=$803
            If EventSource()=wndMain
				quit=True
            End If
	EndIf
	; Always Ontop
	If EventID()=$4001
		ActivateWindow wndMain
		SetGadgetText txt_Gamma,Gamma_Num
	EndIf
	;
	If EventID()=$401
		If EventSource()=SaveBut ; CLICK SAVE BUTTON
			If fileopen=True
				ReadCFG=WriteFile("gamefile.txt")
				For i=1 To linecount
				WriteLine ReadCFG,dat$(i)
				Next
				CloseFile ReadCFG	
			EndIf
		EndIf
		If EventSource()=ExitBut ; CLICK EXIT BUTTON
			quit=True
		EndIf
	EndIf

Until quit

; LABOX CREATION CODE
Function CreateLabox(b_text$,b_x,b_y,b_width,b_height,lab_width,lab_style,b_parent)

	CreatePanel( b_x, b_y, b_width+3, 4, b_parent, 1)                          ; Top
	
	CreatePanel( b_x, b_y+2, 4, b_height, b_parent, 1)                         ; Left
	
	CreatePanel( b_x, b_y + b_height, b_width+4, 4, b_parent, 1)                          ; Bottom
	
	CreatePanel( b_x + b_width, b_y, 4, b_height+3, b_parent, 1)               ; Right
	
	CreateLabel( b_text$, b_x+10, b_y-6, lab_width, 16, b_parent, lab_style)         ; Text
End Function



WolRon(Posted 2003) [#2]
You are setting Gamma_Num to dat$(15). dat$(15) = "gamma=0.5" so converting the string "gamma=0.5" to a number may give you wierd results. You really should set Gamma_Num to equal only the numeral part of dat$(15). To do this you will need to subtract the beginning of the string from it.

Something like: dat$(15) = right$(dat$(15), Len(dat$(15) - 6)

BTW, I'm at work, so if I didnt' write that line correctly, you'll have to figure it out.


Apollonius(Posted 2003) [#3]
wow thanks WolRon ur a really helpful tool ;) (possitive comment)

I think I need to learn what right$, length means and does... as I don't know theses command... however... if i dont put the good value... like if theres 4 numbers... and its changed to 6 numbers how can I know how many numbers there is for the gamma value?

I dont know if u understand what I mean?


soja(Posted 2003) [#4]
you just to find the difference between what the Len$ command returns and where the = is.

Look up Len$ and Instr.


WolRon(Posted 2003) [#5]
dat$(15) = right$(dat$(15), Len(dat$(15) - 6)

The word "gamma" is 5 characters long and the equal sign (=) is one more, so if we subtract 6 characters from the left side of the string, we will only be left with the numeral part.

If the equal sign was not in the 6th position (say, it was in the 7th position (i.e. a space between gamma and the equal sign)) then what I wrote above wouldn't work. It's just a quick and dirty method. If you wanted to know for sure that you only have the numeral part of the string remaining you may have to parse through the line to find which parts you do/don't need.


ford escort(Posted 2003) [#6]
if you have multiple lines where you wan't to change values you can use a procedure that can extract all values of all strings passed to the only req is that the separator is the symbol '='.more generic than using a fixed lenght :)
here's a little exemple
Graphics 800,600
Global Return_field$
value#=extractnumber("gamma_num=0.05")
Text 0,l,"text : "+return_field$:l=l+10
Text 0,l,"value: "+value#:l=l+10

value#=extractnumber("poly_count=555")
Text 0,l,"text : "+return_field$:l=l+10
Text 0,l,"value: "+value#:l=l+10

value#=extractnumber("what do you expect to see here =99980.05")
Text 0,l,"text : "+return_field$:l=l+10
Text 0,l,"value: "+value#:l=l+10

value#=extractnumber("another exemple=500.99")
Text 0,l,"text : "+return_field$:l=l+10
Text 0,l,"value: "+value#:l=l+10

value#=extractnumber("is this helpfull??=4.67")
Text 0,l,"text : "+return_field$:l=l+10
Text 0,l,"value: "+value#:l=l+10

Flip
WaitKey
End
Function extractnumber#(st$)
	Repeat
		b$=Right$(st$,1)+b$
		st$=Left$(st$,Len(st$)-1)
	Until (Right$(st$,1)="=") Or (Len(st$)=0)
	Return_field$=Left$(st$,Len(st$)-1)
	val#=b$
	Return val#
End Function



eni(Posted 2003) [#7]
Just capping off what WolRon and soja have said, I suggest using this:

dat$(15) = Trim$(Mid$(dat$(15),Instr(dat$(15),"=")+1))

This is able to be used for any other values you may have in the future.

Random Eg: "Window Mode=1"

It also works, regardless of formatting (with spaces).

Eg: " Gamma = 2.9 " will still return 2.9


[Edit - Or the above (ford escort) (had left comp then replied later)]