Hi there,
your CoolBasic is really good, but I'm having some trouble doing this:
I want to read an .INI file, which is in a format like
[HEADING1]
var=something
var2=text
etc...
How can I read out a special section (like "var2") using a loop?
I only want to retrieve the string (in this case "text") without the rest.
How can I do this?
Thanks for helping!
How to read INI file
Re: How to read INI file
I'm not sure if anyone has done exactly what you mean. INI-files are somehow complicated.
But this does almost the same thing. Difference is that in this system you cannot have any headers or blocks, but just variables. But usually that is enough.
These functions are copied from http://cbkk.systec.fi/koodi.php?id=29 (only in Finnish) so all thanks go to Bagard
Parameters:
tiedosto$ = file name
otsikko$ = variable name
arvo$ = value of variable
But this does almost the same thing. Difference is that in this system you cannot have any headers or blocks, but just variables. But usually that is enough.
These functions are copied from http://cbkk.systec.fi/koodi.php?id=29 (only in Finnish) so all thanks go to Bagard
Parameters:
tiedosto$ = file name
otsikko$ = variable name
arvo$ = value of variable
Code: Select all
Function WriteConfig(tiedosto$, otsikko$, arvo$)
Dim cfg(100) As String
otsikko$ = Lower(otsikko$)
f = OpenToRead(tiedosto$)
While Not EOF(f)
rivimäärä+1
cfg(rivimäärä) = Lower(ReadLine(f))
Wend
CloseFile f
For i = 1 To rivimäärä
If Left(cfg(i), Len(otsikko$)+1) = otsikko$+"=" Then
cfg(i) = otsikko$+"="+arvo$
muokattu = 1
EndIf
Next i
If Not muokattu Then
rivimäärä+1
cfg(rivimäärä) = otsikko$+"="+arvo$
EndIf
f = OpenToWrite(tiedosto$)
For i = 1 To rivimäärä
WriteLine f, cfg(i)
Next i
CloseFile f
Return 1
End Function
Function ReadConfig(tiedosto$, otsikko$)
otsikko$ = Lower(otsikko$)
If FileExists(tiedosto$) Then
f = OpenToRead(tiedosto$)
While Not EOF(f)
rivi$ = Lower(ReadLine(f))
If Left(rivi$, Len(otsikko$)+1) = otsikko$+"=" Then
arvo$ = Replace(rivi$, otsikko$+"=", "")
Return arvo$
EndIf
Wend
Return 0
Else
MakeError "Config file not found!"
EndIf
End Function
Re: How to read INI file
Thanks, I'll try it in a minute or two...
(Someone should really translate more stuff, especially the documentation...)
(Someone should really translate more stuff, especially the documentation...)
Re: How to read INI file
Manual is partly translated, but not yet really published in English. Some translation files can be found here. Though the translation may have currently stopped because of the stopped/paused develelopment process of whole CoolBasic.rafleo wrote: (Someone should really translate more stuff, especially the documentation...)
But I still think that there should be a proper English manual.
Re: How to read INI file
Hm, I'm still experiencing problems. Somehow it doesn't read my values in the file...
I tried:
I tried everything, but it always says: Can't load image: '' .
If I replace image1=LoadImage(url$) with image1=LoadImage("somefile.png") it works. The "ReadConfig" function seems not to be working...
(I didn't forget to copy the function code into my file, of course)
My config file is constructed like this:
somevariable=somecontent
anothervar=filename.html
etc...
I tried:
Code: Select all
ReadConfig("setup2.ini", "url")
image1=LoadImage(url$)
DrawImage image1,0,0
DrawScreen
If I replace image1=LoadImage(url$) with image1=LoadImage("somefile.png") it works. The "ReadConfig" function seems not to be working...
(I didn't forget to copy the function code into my file, of course)
My config file is constructed like this:
somevariable=somecontent
anothervar=filename.html
etc...
Re: How to read INI file
Try this:
Or like this:
Code: Select all
url$ = ReadConfig("setup2.ini","url")
image1 = LoadImage(url)
DrawImage image1,0,0
DrawScreen
Waitkey
Code: Select all
image1 = LoadImage(ReadConfig("setup2.ini","url"))
DrawImage image1,0,0
DrawScreen
Waitkey
Peli piirtokomennoilla - voittaja, Virtuaalilemmikkipeli - voittaja,
Sukellusvenepeli - voittaja, Paras tileset - voittaja
Vaihtuva päähenkilö - voittaja, Autopeli - voittaja sekä
Hiirellä ohjattava peli - voittaja B)
Sukellusvenepeli - voittaja, Paras tileset - voittaja
Vaihtuva päähenkilö - voittaja, Autopeli - voittaja sekä
Hiirellä ohjattava peli - voittaja B)