] Reading and Writing INI files

Source: Internet
Author: User
Tags key string

Save necessary information in ini
The INI operations are as follows:
==============================================
Setting or storing a small amount of data is often used in the program, so that the program can be used in the next execution, for example, when saving the window position, size, and user-set data during the execution of this program, when programming in DOS, we generally generate a file by ourselves, write the data to the file by yourself, and then read and use it during the next execution. You can also do this in Win32 programming, but Windows has provided us with two convenient methods, that is, using the registry or INI file (profile) to save a small amount of data. This document describes how to use the. ini file.

The INI file is a text file, and the data format in the middle is generally:
[Section1 name]
Keyname1 = value1
Keyname2 = value2
...

[Section2 name]
Keyname1 = value1
Keyname2 = value2

The INI file can be divided into several sections. The names of each section are enclosed in []. In a section, there can be many keys, and each key can have a value and occupy a row, the format is key = value. In Win32 APIs for INI file operations, some are for win. some ini operations are performed on user-defined INI files. Win. In And system. ini are two important initialization files for Windows. Windows records the user's selection and various system information changes in these files. System. ini describes the current status of the system hardware. the win. ini file contains the current configuration of the Windows operating environment. Because of the importance and common usage of the win. ini file, Win32 has APIs dedicated to Windows. ini operations. They are:
& #8226; getprofileint-obtains an integer of the key from a section in the win. ini file. Its original form is:

Getprofileint (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Int ndefault // if the key value is not found, the default value is returned.
);

If the key value is not found, the returned value is the default value specified by ndefault. If the value in the key is negative, 0 is returned. If the key is a combination of numbers and strings, returns the value of the number. For example, if X = 1234 ABCD, 1234 is returned.


& #8226; getprofilestring-obtains a key string from a section in the win. ini file. Its prototype is:

Getprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpdefault, // if the key value is not found, the default string address is returned.
Lptstr lpreturnedstring, // return the buffer address of the string
DWORD nsize // buffer Length
);

The returned string is in the buffer, and the returned eax value is the length of the returned string (excluding the end 0)


& #8226; getprofilesection-read the content of the entire section from the win. ini file. Its original form is:

Getprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpreturnedstring, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
);


& #8226; writeprofilesection-Write the value of an entire section into the specified section of the win. ini file. Its prototype is:

Writeprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpstring // the address of the data to be written
);

If the section is not specified by win. ini, the API creates a new section and writes data. If the Section already exists, delete all the key values in the original seciton and write the new section.


& #8226; writeprofilestring-write a key value to the specified section of the win. ini file. Its prototype is:

Writeprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpstring // string address to be written
);

If no section is specified for win. ini, the API creates a section. If no key is specified, a new key is created and data is written. If yes, a string is used to replace the original value.

The above API is for win. INI operations, of course, for us, we use more to create our INI files in the directory where the program runs. If you need to operate on your INI files, another group of APIS is required. This group of APIS is similar to the above one. You only need to change the profile of the above group to privateprofile (private, the parameter also has an INI file name parameter. For example, getprivateprofileint, getprivateprofilesection, and writeprivateprofilestring are described as follows:
& #8226; getprivateprofileint-obtains an integer of the key from a section in the INI file. Its original form is:

Getprivateprofileint (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Int ndefault // if the key value is not found, the default value is returned.
Name of the lpfilename // INI File
);

The definitions of intermediate parameters and returned values are the same as those of getprofileint.


& #8226; getprivateprofilestring-obtains a key string from a section of the INI file. Its prototype is:

Getprivateprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpdefault, // if the key value is not found, the default string address is returned.
Lptstr lpreturnedstring, // return the buffer address of the string
DWORD nsize // buffer Length
Name of the lpfilename // INI File
);


& #8226; getprivateprofilesection-read the content of the entire section from the INI file. Its original form is:

Getprivateprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpreturnedstring, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
Name of the lpfilename // INI File
);

This API can read the content of the entire section. If you do not know the keys in the section, you can use this API to read the entire section and then process it.


& #8226; getprivateprofilesectionnames-obtain the section name from the INI file. Its original form is:

Getprivateprofilesectionnames (
Lptstr lpszreturnbuffer, // The buffer address of the returned data
DWORD nsize // The buffer length of the returned data
Name of the lpfilename // INI File
);

If ini contains two sections: [sec1] and [sec2], the returned sections are 'sec1', 0, 'sec2, you can use this API to obtain the name of a section in ini.


& #8226; writeprivateprofilesection-Add the content of an entire section to the specified section of the INI file. Its prototype is:

Writeprivateprofilesection (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpstring // the address of the data to be written
Name of the lpfilename // INI File
);


& #8226; writeprivateprofilestring-write a key value to the specified section of the INI file. Its prototype is:

Writeprivateprofilestring (
Lptstr lpappname, // point to the string address containing the section name
Lptstr lpkeyname, // point to the string address containing the key name
Lptstr lpstring // string address to be written
Name of the lpfilename // INI File
);

If no section is specified in ini, the API creates a section. If no key is specified, a new key is created and data is written. If yes, the original value is replaced by a string. When the specified ini does not exist, the API will automatically create a new file, so the advantage of using INI is that we do not have to involve file operations to save a small amount of data, it is unnecessary to search for the existence of a file.

Usage tips:

In actual use, getprivateprofilestring and writeprivateprofilestring are used most. However, when operating on a custom INI file, note that if the file specified by lpfilename has no path, the API will go to the installation directory of Windows instead of the current directory, but it is too troublesome to get the current path every time you use the INI function. Here is a work und, you only need to add.. INI operation, the file name is '. /user. INI 'is obviously more convenient. In addition, to clear a key, you can point lpstring to an empty string and then use writeprivateprofilestring. When you want to clear all the content of a section, you do not need to clear the keys one by one. You can point the lpstring to an empty string and then use writeprivateprofilesection.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.