How to read INI file

Post your coding questions such as 'how to' here.
Post Reply
rafleo

How to read INI file

Post by rafleo »

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!
rafleo

Re: How to read INI file

Post by rafleo »

no-one an idea?
User avatar
Jare
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori
Contact:

Re: How to read INI file

Post by Jare »

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

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
rafleo

Re: How to read INI file

Post by rafleo »

Thanks, I'll try it in a minute or two...

(Someone should really translate more stuff, especially the documentation...)
User avatar
Jare
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori
Contact:

Re: How to read INI file

Post by Jare »

rafleo wrote: (Someone should really translate more stuff, especially the documentation...)
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. :(

But I still think that there should be a proper English manual.
rafleo

Re: How to read INI file

Post by rafleo »

Hm, I'm still experiencing problems. Somehow it doesn't read my values in the file...

I tried:

Code: Select all

ReadConfig("setup2.ini", "url") 

image1=LoadImage(url$)
DrawImage image1,0,0

DrawScreen
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...
Harakka
Advanced Member
Posts: 430
Joined: Mon Aug 27, 2007 9:08 pm
Location: Salo
Contact:

Re: How to read INI file

Post by Harakka »

Try this:

Code: Select all

url$ = ReadConfig("setup2.ini","url")
image1 = LoadImage(url)
DrawImage image1,0,0
DrawScreen
Waitkey
Or like this:

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)
rafleo

Re: How to read INI file

Post by rafleo »

Thanks a lot, first example works fine.
Post Reply