Among the procedures we wrote, there are always some configuration information to be saved so that the functionality of the program is to write this information to the INI file. The program is initialized. The specific application is as follows:
1. Write the information in the .ini file.
1. The WinAPI function used in:
Bool WritePrivateProfileString (LPCTSTSTSTR LPKPNAME, LPCTSTR LPSTRING, LPCTSTSTR LPFILENAME);
The meaning of each parameter:
LPCTSTR LPAPPNAME is a field name in the INI file.
LPCTSTR LPKEYNAME is a key name under LPAppname, which is a variable name.
LPCTSTR LPSTRING is a key value, that is, the value of the variable, but must be a LPCTSTR or CSTRING type.
LPCTSTR LPFILENAME is a complete INI file name.
2. Specific use: Set up an existing student, you need to write his name and age into the C: /Stud/student.ini file.
CSTRING STRNAME, STRTEMP; INT NAGE; Strname = "Zhang 3"; NAGE = 12; :: WritePrivateProfileString ("StudentInfo", "Name", Strname, "C: //stud/student.ini");
The content in the c: /stud/student.ini file is as follows:
[Studentinfo] name = Zhang 3
3. To save the age of the students, simply try the value of the integer:
Strtemp.format ("% D", NAGE); :: WritePrivateProfileString ("StudentInfo", "AGE", Strtemp, "C: //stud/student.ini"); II. Read the information from the INI file Variables in the program.
1. The WinAPI function used in:
DWORD GETPRIVATEPROFILESTRING (LPCTSTSTR LPAPPNAME, LPCTSTR LPKEYNAME, LPCTSTSTR LPDEFAULT, LPTSTSTSTSTR LPRETURNEDSTRING, DWORD NSIZE, LPCTSTSTR LPFILENAME);
The meaning of each parameter:
The first two parameters are the same as the meaning of WritePrivateProfileString.
LPDEFAULT: If there is no previous two parameter specified in the INI file, the value is assigned to the variable.
LPRETURNEDSTRING: The CString object that receives the value in the INI file, that is, the destination buffer.
Nsize: The size of the destination buffer.
LPFileName: is a complete INI file name.
2. Specific use: Now use the information of the student 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 stratudname is: "Zhang San", if the first two parameters are incorrect, its value is: "Default Name".
3. Read the integer value to use another WinAPI function:
Uint getPrivateProfileint (LPCTSTR LPAPPNAME, INT NDEFAULT, LPCTSTR LPFILENAME);
The parameters here are the same. The method is as follows:
NStudage = GetPrivateProfileint ("StudentInfo", "Age", 10, "C: //stud/student.ini"); III. Recycling multiple values, set there is a program, and several files to be used recently The name is saved, the specific procedures are as follows: 1. Write:
CSTRING STRTEMP, STRTEMPA; INT i; int ncount = 6; file: // There is a total of 6 file names need to save for (i = 0; i {straTemp.format ("% d", i); strTempa = file name; File : // The file name can be obtained from an array, list box, etc. :: WritePrivateProfileString ("Usefilename", "FileName" Strtemp, Strtempa, "C: //Usefile//usefile.ini");} Strtemp.Format "% d", ncount) ;: WritePrivateProfileString ("FileCount", "Count", Strtemp, "C: //Usefile//usefile.ini"); file: // Writes the total number of files to read.
2. Read:
Ncount = :: getPrivateProfileint ("FileCount", "Count", 0, "c: //usefile//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/Usefile.ini");
File: // Use the contents of Strtempa.
}
Add four points:
The path to the 1.ini file must be complete, and the file names must exist in front of the file name, otherwise the write is unsuccessful, and the function returns the false value.
2. The file name must be //, because in VC , // represents one /.
3. You can also put the INI file in the directory where the program is located, and the LPFileName parameter is: ".//student.ini".