Among the programs we write, there is always some configuration information to be saved to complete the functions of the program. The simplest way is to write the information into the INI file and then read it during program initialization. the specific application is as follows:
1. Write information to the. ini file.
1. the prototype of the winapi function used is:
Bool writeprivateprofilestring (
Meanings of parameters:
Lptstr lpappname is a field name in the INI file.
Lptstr lpkeyname is a key name under lpappname. Generally, it is a variable name.
Lpstring is the key value, that is, the value of the variable. However, the value must be of the lpctstr or cstring type.
Lptstr lpfilename is the complete INI file name.
2. Specific usage: If you have a student, you need to write his name and age to the C: \ stud \ student. ini file.
Cstring strname, strtemp;
The content in the C: \ stud \ student. ini file is as follows:
[Studentinfo]
3. To save the student's age, you only need to change the integer value to the balanced type:
Strtemp. Format ("% d", Nage );
2. Read the information from the INI file into the variables in the program.
1. the prototype of the winapi function used is:
DWORD getprivateprofilestring (
Meanings of parameters:
The first two parameters have the same meaning as writeprivateprofilestring.
Lpdefault: If the INI file does not contain the field name or key name specified by the first two parameters, this value is assigned to the variable.
Lpreturnedstring: The cstring object that receives the value in the INI file, that is, the destination cache.
Nsize: the size of the destination cache.
Lpfilename: the complete INI file name.
2. Usage: Read the student information written in the previous step into the program.
Cstring strstudname;
After execution, the value of strstudname is "Zhang San". If the first two parameters are incorrect, the value is "default name ".
3. Use another winapi function to read integer values:
Uint getprivateprofileint (
The parameters here have the same meaning as above. The usage is as follows:
Nstudage = getprivateprofileint ("studentinfo", "Age", 10, "C: \ stud \ student. ini ");
3. Write multiple values cyclically. If you have a program, you need to save several recently used file names. The specific program is as follows:
1. Write:
Cstring strtemp, strtempa;
2. Read:
Ncount =: getprivateprofileint ("filecount", "Count", 0, "C: \ usefile. ini ");
File: // use the content in strtempa.
}
Four additional points:
1. The path of the INI file must be complete, and directories at all levels before the file name must exist. Otherwise, the write operation fails and the function returns false.
2. The file name path must be \, because in VC ++, \ indicates \.
3. You can also put the INI file in the directory where the program is located. In this case, the lpfilename parameter is: ". \ student. ini ".