Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save a key to an INI file

Status
Not open for further replies.

winline

Programmer
Dec 30, 2002
13
AU
I am trying to make a function were you can save a key to an INI file.

But it only works if the [section] doesn't exist.

Here is the code and I hope you can help me on this one!

FUNCTION saveINIkey$ (file$, section$, key$, value$)
what$ = "[" + section$ + "]"
OPEN file$ FOR APPEND AS #1
CLOSE #1

what = LEN(section$) + 2

OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
INPUT #1, M$(C)
IF LEFT$(M$(C), what) = what$ THEN lne = C
C = C + 1
LOOP
CLOSE #1

IF lne = 0 THEN
OPEN file$ FOR APPEND AS #1
PRINT #1, what$
PRINT #1, key$ + &quot;=&quot; + value$
CLOSE #1
EXIT FUNCTION
END IF

END FUNCTION
 
well, it will only work if the section you are writing it to doesn't exist.

I need this changed so if the section DOES exist yet the key not it will ad it after the section starts

And then if the section exists and the key exists that it would overite the key with the new key provided when you call the sub.

I need help on this one!
Tim Groeneveld,
13
 
0. set section heading line number to 0.
set key line number to 0.
1. Scan through all the file$.
if found, store (section heading line number) and (key line number).
2. If (key line number)=0 and (section heading line number)=0
proceed as your previous code did.
3. If (key line number)=0 and (section heading line number)<>0
open file$ for input while some temporary for output
copy (read line from one file - write to another one,
line input/print) lines till (section heading line number)
write new pair &quot;key=value&quot;
copy rest of the file$ to temporary file
rename file$ to backup copy (or delete it if you don't care)
rename temporary file to file$
4. If (key line number)<>0 and (section heading line number)<>0
proceed almost as (3);
more specific, copy files till (key line number)-1
then write new pair &quot;key=value&quot;
and read old one (but not write it - just let it be there)
then copy rest of the file.
and rename as in previous case.

Hope this helps.
 
It works -==MOSTLY==- ;)

I have everything working ONLY if it is called twice. Here is the code so far:

DECLARE FUNCTION saveINIkey$ (file$, section$, key$, value$)
DECLARE FUNCTION getINIkey$ (file$, section$, key$)

CLS
CLEAR 3000
tim$ = saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;Copy&quot;, &quot;(C) 2002 WNLINE Networks&quot;)

FUNCTION getINIkey$ (file$, section$, key$)
what$ = &quot;[&quot; + section$ + &quot;]&quot;
OPEN file$ FOR APPEND AS #1
CLOSE #1

what = LEN(section$) + 2


OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
INPUT #1, M$(C)
IF LEFT$(M$(C), what) = what$ THEN lne = C
C = C + 1
LOOP
CLOSE #1
IF lne = 0 THEN
EXIT FUNCTION
END IF

UT = lne
kl = LEN(key$)
DO WHILE UT < C
IF LEFT$(M$(UT), kl) = key$ THEN
temp$ = RIGHT$(M$(UT), LEN(M$(UT)) - kl - 1)
getINIkey$ = temp$
EXIT FUNCTION
END IF
UT = UT + 1
LOOP


END FUNCTION

FUNCTION saveINIkey$ (file$, section$, key$, value$)
what$ = &quot;[&quot; + section$ + &quot;]&quot;
OPEN file$ FOR APPEND AS #1
CLOSE #1

what = LEN(section$) + 2
wat2 = LEN(key$)


OPEN file$ FOR INPUT AS #1
C = 1
DIM M$(1 TO 1000)
DO WHILE NOT EOF(1) AND C <= 1000
INPUT #1, M$(C)
IF LEFT$(M$(C), wat2) = key$ THEN kln = C
IF LEFT$(M$(C), what) = what$ THEN lne = C
C = C + 1
LOOP
CLOSE #1

' ***************************************************************
' * START HERE THE CHEAKING IF BOTH THE SECTION AND KEY DOESN'T *
' * EXIST IN THE INI FILE. *
' ***************************************************************
IF lne = 0 AND kln = 0 THEN
OPEN file$ FOR APPEND AS #1
PRINT #1, what$
PRINT #1, key$ + &quot;=&quot; + value$
CLOSE #1
GOTO xlear
END IF

' ***************************************************************
' * START HERE THE CHEAKING IF THE SECTION DOES EXIST BUT THE *
' * KEY DOES EXIST IN THE INI FILE. *
' ***************************************************************

IF lne <> 0 AND kln = 0 THEN
OPEN file$ FOR INPUT AS #1
DIM OLD$(1 TO 1000)
DIM NEW$(1 TO 1000)
C = 1
N = 1
DO WHILE NOT EOF(1) AND C <= 1000
INPUT #1, OLD$(C)
NEW$(N) = OLD$(C)
IF LEFT$(OLD$(C), what) = what$ THEN
N = N + 1
NEW$(N) = key$ + &quot;=&quot; + value$
END IF
C = C + 1
N = N + 1
LOOP
CLOSE #1
OPEN file$ FOR OUTPUT AS #1
Y = 1
DO WHILE Y <> N
PRINT #1, NEW$(Y)
Y = Y + 1
LOOP
CLOSE #1
GOTO xlear
END IF

' ***************************************************************
' * START HERE THE CHEAKING IF THE SECTION DOES EXIST AND THE *
' * KEY DOES EXIST IN THE INI FILE TO. *
' ***************************************************************

IF lne <> 0 AND kln <> 0 THEN
OPEN file$ FOR INPUT AS #1
DIM OLD$(1 TO 1000)
C = 1
DO WHILE NOT EOF(1) AND C <= 1000
INPUT #1, OLD$(C)
IF LEFT$(OLD$(C), wat2) = key$ THEN
OLD$(C) = key$ + &quot;=&quot; + value$
END IF
C = C + 1
LOOP
CLOSE #1
OPEN file$ FOR OUTPUT AS #1
Y = 1
DO WHILE Y <> C
PRINT #1, OLD$(Y)
Y = Y + 1
LOOP
GOTO xlear
END IF
CLOSE #1

xlear:
ERASE NEW$, OLD$, M$
END FUNCTION

Could someone please have a look at this code please and see what is the problem?
 
Using QB 4.5 on ME. I ran it once and had no problems. There are some sections of code that could be reduced or streamlined, and some where I quite frankly don't know that you are trying to accomplish (such as in your GETINIKEY, you create [append] a file with nothing in it? -- you should checkout the privious example I gave to Bali on thread314-454888.) But mine's not to reason &quot;Why&quot;.

The only thing I can think of is that you are using
tim$ = saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;Copy&quot;, &quot;(C) 2002 WNLINE Networks&quot;)
Yet, you have not told the function to RETURN ANYTHING in place of SaveIniKey. Here add the stuff in red.

In your main module add:

>CLS
>CLEAR 3000
>tim$ = saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;Copy&quot;, &quot;(C) 2002 WNLINE Networks&quot;)
[red]print &quot;Results: &quot;; Tim$[/red]

RUN IT NOW...and you'll see it will return nothing.


In your saveINIkey$ FUNCTION add:

>xlear:
>ERASE NEW$, OLD$, M$
[red]saveINIkey$ = &quot;OK SAVED&quot;[/red]
>END FUNCTION

RUN IT NOW...and you'll see it will return something.


If this is not the problem, please state it clearly on your next posting.

--MiggyD
 
Thats not the problem.
Sorry - my english isn't very good so I find it a bit hard to descipe what I mean. Do forgive me ;)

I know that Tim$ will return nothing.

It is after I have run:
saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;Copy&quot;, &quot;(C) 2002 WNLINE Networks&quot;)
saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;this&quot;, &quot;was a fake message&quot;)
saveINIkey(&quot;tim.ini&quot;, &quot;Boot&quot;, &quot;this&quot;, &quot;now another message&quot;)

(NOTE: The above was an example) if I have run that twice, it seems to crach the file and the text in the ini file says:
this=was a fake message

were it should read:
this=now another message

??? Do you know what the prob is? ???
Tim
 
to:winline

At you code, line 88 in FUNCTION saveINIkey$ (marked in red)

[tt]
OPEN file$ FOR OUTPUT AS #1
Y = 1
DO WHILE Y <> C
PRINT #1, OLD$(Y)
Y = Y + 1
LOOP
GOTO xlear
END IF
CLOSE #1
[/tt]

You see - you are going to label xlear and leave the file un-closed!
Hense the error message &quot;file already open&quot; next time.

Hope this helps.
 
Kind of sounds like a caching problem. The file is stored in memory and not to disk until you tell it to close the file or the program terminates--forcing it to close.

--MiggyD
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top