User Tools

Site Tools


reading_20and_20writing_20.ini_20data_20files

Reading and writing .INI data files

by Richard Russell, May 2006

You may want your program to read configuration settings or user options on start up, to avoid the user having to enter them every time. One way of doing that is to use the registry, and this is described in the main help documentation here.

However there may be reasons why you prefer not to use the registry, for example you might like the configuration settings to be in a file which can be easily read and edited, and even transferred between different computers. In that case you can store the data in a .INI file, which is a text file in a standardised format. Here is an example of a .INI file:

  [settings]
  aspect=1
  scaling=1
  filmise=0
  aperture=3
  blanking=0
  scroll=0
  henhance=0
  venhance=0
 
  [codec]
  compressor=mp4v
  quality=512

The file consists of a number of sections (here they are settings and codec) and within each section are one or more lines in the format “key=value”. Windows makes it very easy to use such files because it provides API routines to access them.

To read data from a .INI file you can use the following function:

        DEF FNgetinistring(file$, section$, key$)
        LOCAL buf%
        DIM buf% LOCAL 255
        SYS "GetPrivateProfileString", section$, key$, "", buf%, 256, file$
        = $$buf%

You would call that from your program in the following way, using the above file (called ARCQTM.INI) as an example:

        aperture = VAL(FNgetinistring(@usr$+"ARCQTM.INI", "settings", "aperture"))
        compressor$ = FNgetinistring(@usr$+"ARCQTM.INI", "codec", "compressor")

Here it is assumed that the file resides in the user's Documents folder. You should always specify a full path to the file.

Arguably, a better place to store the file would be the user's Application Data folder, which you can do as follows:

        appdata$ = FNspecialfolder(26)
        aperture = VAL(FNgetinistring(appdata$+"ARCQTM.INI", "settings", "aperture"))
        compressor$ = FNgetinistring(appdata$+"ARCQTM.INI", "codec", "compressor")

The FNspecialfolder routine is listed in the main Help documentation here.

The above examples retrieve the data as a string. If you know that the data will be an integer, then a simpler form can be used:

        SYS "GetPrivateProfileInt", section$, key$, default%, file$ TO aperture%

The value default% is returned in the case where the section or key does not already exist in the .INI file, such as when first running a program. There is no equivalent system call for writing of integers and the method below should be used.

To write data to (and if necessary create) a .INI file you can use the following procedure:

        DEF PROCputinistring(file$, section$, key$, info$)
        LOCAL res%
        SYS "WritePrivateProfileString", section$, key$, info$, file$ TO res%
        IF res% = 0 ERROR 100, "Couldn't write to file "+file$
        ENDPROC

You would call that from your program as follows:

        PROCputinistring(appdata$+"ARCQTM.INI", "settings", "aperture", STR$(aperture))
        PROCputinistring(appdata$+"ARCQTM.INI", "codec", "compressor", compressor$)
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
reading_20and_20writing_20.ini_20data_20files.txt · Last modified: 2024/01/05 00:21 by 127.0.0.1