In the program we write, there is always some configuration information that needs to be saved to complete the program function. 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 (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpstring,
Lptstr lpfilename
);
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;
Int Nage;
Strname = "James ";
Nage = 12;
: Writeprivateprofilestring ("studentinfo", "name", strname, "C: // stud // student. ini ");
The content in the C:/stud/student. ini file is as follows:
[Studentinfo]
Name = James
3. To save the student's age, you only need to change the integer value to the balanced type:
Strtemp. Format ("% d", Nage );
: Writeprivateprofilestring ("studentinfo", "Age", strtemp, "C: // stud // student. ini ");
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 (
Lptstr lpappname,
Lptstr lpkeyname,
Lptstr lpdefault,
Lptstr lpreturnedstring,
DWORD nsize,
Lptstr lpfilename
);
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;
Int nstudage;
Getprivateprofilestring ("studentinfo", "name", "default name", strstudname. getbuffer (max_path), max_path, "C: // stud // student. ini ");
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 (
Lptstr lpappname,
Lptstr lpkeyname,
Int ndefault,
Lptstr lpfilename
);
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;
Int I;
Int ncount = 6;
File: // there are 6 file names to save
For (I = 0; I {strtemp. Format ("% d", I );
Strtempa = file name;
File: // the file name can be obtained from an array or a list box.
: Writeprivateprofilestring ("usefilename", "FILENAME" + strtemp, strtempa, "C: // usefile. ini ");
}
Strtemp. Format ("% d", ncount );
: Writeprivateprofilestring ("filecount", "Count", strtemp, "C: // usefile. ini ");
File: // write the total number of files for reading.
2. Read:
Ncount =: getprivateprofileint ("filecount", "Count", 0, "C: // usefile. ini ");
For (I = 0; I {strtemp. Format ("% d", I );
Strtemp = "FILENAME" + strtemp;
: Getprivateprofilestring ("currentini", strtemp, "Default. fil", strtempa. getbuffer (max_path), max_path, "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. The lpfilename parameter is: ". // student. ini ".
4. when pasting source code from a Web page, it is best to paste the source code to notepad first, and then paste it to VC. Otherwise, compilation errors may easily occur, and I will not be able to understand it at the beginning. How can this problem be solved if the code is good? Later, I found this method. Some codes also use full-width characters such as: <, \, which may cause compilation errors.