ini file usage

Share your advanced PureBasic knowledge/code with the community.
User avatar
waffle
Enthusiast
Enthusiast
Posts: 129
Joined: Mon May 12, 2003 1:34 pm
Location: USA
Contact:

ini file usage

Post by waffle »

after tinkering with the OpenPreferences function, i found it lacks some of the functionality of the api calls. So i converted my code to use the api.

this applies to windows only. Fred probably did his function to support linix and windows, which would explain the problem. The exact problem was that once i created my pref file, i could not make changes unless i deleted the file. The api does not require that. Here is how to use the api:

Code: Select all

#BufferSize=1000
Global *FileBuffer ;so it can be used inside any function
*FileBuffer = AllocateMemory(#BufferSize)

;other vars used by API
;these can be declared inside functions as required
IniFileName.s="MyIni.INI"
Section.s="First User"
KeyName.s="First Key"

;write a string to the ini file
WritePrivateProfileString_(Section, KeyName, "Data!", IniFileName)

;Read a string
GetPrivateProfileString_(Section, KeyName, "Default", *FileBuffer, MemorySize(*FileBuffer), IniFileName)
KeyData.s=PeekS(*FileBuffer)
Debug KeyData

;Read a long or int
KeyName="Key Int"
KeyVal.l=GetPrivateProfileInt_(Section,KeyName, 0, IniFileName)

;Write a long or int(this gets slightly odd)
WritePrivateProfileString_(Section,KeyName,Str(KeyVal),IniFileName)
I find this method easier than freds method, but his will also support linix while this will not.
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post by naw »

Yep - I had the same problem with Windows last year & reported it under Bugs, but I guess its a feature not a bug ;-)

- if you use the Preferences Library for *doing* INI files, and Open a file for Writing, the first time you write an entry, it removes everything else. I would have expected it to overwrite the entry (not trash the file) - ho-hum!

The Windows API commands don't suffer the same problem...
Ta - N
Post Reply