
How to read and write an ini file with Qbasic 4.5
Quote:
> Ok, i already knows that, what I need is do you have a fonction that write
> and read ini easily like
> filename$="something.ini"
> section$="Test"
> key$="writing"
> Value$=Message$
> Gosub Writeini
> and
> filename$="something.ini"
> section$="Test"
> Key$="Reading"
> Gosub ReadIni
> you see what I mean?
> Fred
Here is a simple demo for the ReadINI function. Add your own error checking
to the function as required. Now see if you can manage the WriteINI function
on your own. Hint; it's a bit more involved. The file has to be read until
either the section and key is found, or append a new section with the key.
The trick is, you have to write to a temporary file, then replace the
original with the temporary. I recommend you practice writing with a dummy
.ini file. Good luck with the exercise.
BTW, you can do all this work in memory with a string variable/array, but,
considering you should only need to read during program initialization,
write only when closing, and .ini files are small anyway, the gain is
minimal.
DECLARE FUNCTION ReadINI$ (FileName$, Section$, Key$)
CLS
FileName$ = "c:\windows\win.ini"
Section$ = "desktop"
Key$ = "wallpaper"
Value$ = ReadINI$(FileName$, Section$, Key$)
PRINT "Your wallpaper is ";
IF LEN(Value$) THEN
PRINT Value$
ELSE
PRINT "not set!"
END IF
SYSTEM
FUNCTION ReadINI$ (FileName$, Section$, Key$)
FF = FREEFILE
OPEN FileName$ FOR INPUT AS #FF
INILOF = LOF(FF)
IF INILOF THEN
Section$ = UCASE$(Section$)
Key$ = UCASE$(Key$)
DO UNTIL EOF(FF)
LINE INPUT #FF, in$
in$ = LTRIM$(in$)
IF SectionFound THEN
'Search for the key within section
IF LEFT$(UCASE$(in$), LEN(Key$) + 1) = Key$ + "=" THEN
'Key found so set return string to the function
ReadINI = MID$(in$, LEN(Key$) + 2)
EXIT DO
END IF
END IF
IF LEFT$(in$, 1) = "[" THEN
'It's a section
IF SectionFound THEN
'Key was not found within the section
EXIT DO
ELSE
'Check if it's the correct section
SectionFound = INSTR(UCASE$(in$), "[" + Section$ + "]")
END IF
END IF
LOOP
END IF
CLOSE #FF
END FUNCTION
--
Todd Vargo (body of message must contain my name to reply by email)